Reputation: 2254
I would like to get a list of fast changing files which are newer than a timestamp.
The files I would especially like to follow are temporary files which are renamed after they have been completely downloaded.
In my first trial, I made the problem separable, i.e., first (1) listed the files and then (2) tried to look their modification times:
import os.path
import glob
import datetime
def get_newer_files(ref_time = '2020-05-02 16:27:00.000000'):
path = os.path.expanduser("~") + '\\Downloads\\'
files = [f for f in glob.glob(path + "*.*")]
selected_files = []
for f in files:
dt = os.path.getmtime(f)
dt_string = str(datetime.datetime.fromtimestamp(dt))
if (dt_string > ref_time):
selected_files += [f]
return selected_files
However, the separable approach yields sometimes FileNotFoundError
, because a temporary file may have disappeared after it has first been listed.
Is there a neat way to consistently list files which are newer than a certain timestamp, without a possibility to end up into such errors?
Upvotes: 1
Views: 364
Reputation: 9494
Try to use scandir()
which returns an iterator of all the objects in a directory including file attribute information:
from os import scandir
from datetime import datetime
dir_entries = scandir('.')
for entry in dir_entries:
if entry.is_file():
file_name = entry.name
last_modified = datetime.utcfromtimestamp(entry.stat().st_mtime)
print(file_name, last_modified)
# you can filter here unwanted files older than X
Upvotes: 1