generate functionSat, 12 Nov 2022

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

import os import time import datetime #create a log file with the time logFileName = "d:/tdarrremoved.txt" with open(logFileName, 'a') as f: f.write("{}".format(datetime.datetime.now())) f.write("\n") d = "d:/" for f in os.listdir(d): if f.endswith(".tdarrcache"): fileTime = os.path.getmtime(d+f) twoDaysAgo = time.time() - (60 * 60 * 24 * 2) if fileTime < twoDaysAgo: os.remove(d+f) with open(logFileName, 'a') as f: f.write("{}".format(f)) f.write("\n")

Questions about programming?Chat with your personal AI assistant