I am trying to remove files in my directory if they are over 30 days. However I don't know what other checks I need to do. The examples I found online wasn't very thorough besides just subtracting the datetime of when the file was created against X amount of days.
import datetime import os def remove_old_files(): """ Remove any file over 30 days """ file_path_directory = "inter/" today = datetime.datetime.now() retention_days = 30 try: # Check each item in our path and verify if it's a file. for each_file in os.listdir(file_path_directory): each_file_path = os.path.join(file_path_directory, each_file) if os.path.isfile(each_file_path): # Get created datetime for each file and subtract the days from today. file_creation_date = datetime.datetime.fromtimestamp(os.path.getctime(each_file_path)) number_of_days = today - file_creation_date if number_of_days.days > retention_days: os.remove(each_file_path) except Exception as e: print("Exception in remove_old_files: ", e)