generate functionSat, 12 Nov 2022

Find files with extension .tdarrcache* on d:/. If the files are over two days old, delete them.

import os import datetime now = datetime.datetime.now() def find_and_delete(path,ext,age): for root, dirs, files in os.walk(path): for file in files: fname = os.path.join(root, file) asplit = os.path.splitext(fname) file_ext = asplit[1] file_time = datetime.datetime.fromtimestamp(os.path.getmtime(fname)) if file_ext == ext and (now - file_time).days > age: os.remove(fname) print('file deleted') else: print('file not deleted') find_and_delete('d:/', 'tdarrcache', 2)

Questions about programming?Chat with your personal AI assistant