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.

#!/usr/bin/env python import os import time import datetime import shutil # Set the directory you want to start from rootDir = 'D:\\' # Delete files and log def deleteFile(fileName, filePath): print(fileName + ' deleted') fileToDelete = filePath + fileName os.remove(fileToDelete) with open('D:\\tdarrremoved.txt','a') as f: f.write('\n' + fileName + ' deleted.') for dirName, subdirList, fileList in os.walk(rootDir): for fname in fileList: if fname.endswith('tdarrcache'): filePath = dirName + '\\' fileName = fname modTime = time.ctime(os.path.getmtime(filePath + fileName)) modTimeSeconds = os.path.getmtime(filePath + fileName) currentTimeSeconds = time.time() time

Questions about programming?Chat with your personal AI assistant