Nov-09-2021, 01:10 PM
Chain comparison operators and don't compare if datetimes are equal.
import datetime import os def iterdirs(root, min_days_age=0, max_days_age=1): now = datetime.datetime.now() # not newer than end_date end_date = now - datetime.timedelta(days=min_days_age) # not older than start_date start_date = now - datetime.timedelta(days=max_days_age) for path in os.listdir(root): if not os.path.isdir(path): # if path is not a directory, skip it # processing only directories continue mtime = datetime.datetime.fromtimestamp(os.path.getmtime(path)) # chaining comparison operators: # https://www.geeksforgeeks.org/chaining-comparison-operators-python/ if start_date <= mtime <= end_date: yield path
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!