How can I find files with filtering according to being newer than some query date in Python without crawling the entire search directory? E.g., in Bash/*nix (tested on MacOS) I can do find . -newermt '2018-01-17 03:28:46'
, which quickly searches for the files just newer than the specified query date. In Python I can do:
import os
import datetime
query_date = datetime.datetime.fromtimestamp(int(float(1516188526532974000)/1000000000))
results = []
for root, dirs, files in os.walk('/Users/Nafty/Sync/sxs'):
for filename in files:
path = os.path.join(root, filename)
file_mtime = datetime.datetime.fromtimestamp(os.stat(path).st_mtime)
if(file_mtime > query_date):
results.append(path) # yield path?
return results
However, this takes longer and seems to walk through the entire directory regardless.
Is there a way to do a fast search version of date-filtered directory crawling in Python, similar to the Bash example?