generate functionSun, 13 Nov 2022

Delete files named *.tdarrcache* in D:\sonarr\ and D:\radarr\ only if they are over 2 days old.

#!/usr/bin/env python3 # Imports import os import datetime as dt # Set directory to search folder = [r'D:\radarr', r'D:\sonarr'] # Set file extension to search for extension = r'*.tdarrcache' # Set age of files to delete age_in_days = 2 def delete_old_files(folder, extension, age_in_days): today = dt.datetime.today() files = [] for root, dirs, filenames in os.walk(folder): for filename in fnmatch.filter(filenames, extension): files.append(os.path.join(root, filename)) for file in files: if (today - dt.datetime.fromtimestamp(os.path.getmtime(file))).days >= age_in_days: os.remove(file) print('Delete: {}'.format(file)) for f in folder: delete_old_files(f, extension

Questions about programming?Chat with your personal AI assistant