I have a cron job that deletes files based on their age using the command:
find /path/to/file/ -type f -mmin +120|xargs -I file rm \'file\'
My method would be:
import os
import time
def checkfile(filename):
filestats = os.stat(filename) # Gets infromation on file.
if time.time() - filestats.st_mtime > 120: # Compares if file modification date is more than 120 less than the current time.
os.remove(filename) # Removes file if it needs to be removed.
path = '/path/to/folder'
dirList = os.listdir(path) # Lists specified directory.
for filename in dirList:
checkfile(os.path.join(path, filename)) # Runs checkfile function.
Edit: I tested it, it didn't work, so i fixed the code and i can confirm it works.