Os.walk is used for a recursive directory search . For each file, it checks the changed date using os.path.getmtime and compares this with datetime.now (current time). datetime.timedelta is built to create a timedelta in 24 hours.
It looks for the os.path.curdir directory, which is the current directory when the script is called. You can set dir_to_search to something else, for example. script parameter.
import os import datetime dir_to_search = os.path.curdir for dirpath, dirnames, filenames in os.walk(dir_to_search): for file in filenames: curpath = os.path.join(dirpath, file) file_modified = datetime.datetime.fromtimestamp(os.path.getmtime(curpath)) if datetime.datetime.now() - file_modified > datetime.timedelta(hours=24): os.remove(curpath)
jterrace
source share