generate functionSat, 12 Nov 2022

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

import os, datetime, time, re def remove_files(path): os.chdir(path) now = datetime.datetime.now() days_ago = now - datetime.timedelta(days=2) days_ago_epoch = time.mktime(days_ago.timetuple()) days_ago_readable = time.ctime(days_ago_epoch) files = os.listdir(path) for f in files: if os.path.isfile(f) and re.match('.*\.tdarrcache.*', f): file_epoch = os.path.getmtime(f) if file_epoch < days_ago_epoch: os.remove(f) with open('d:/tdarrremoved.txt', 'a') as log: log.write('removed ' + f + ' on ' + days_ago_readable + '\n') remove_files('d:/')

Questions about programming?Chat with your personal AI assistant