import os import time MAX_AGE = 60 * 60 # 1 hour def cleanup(folder="jobs"): now = time.time() for f in os.listdir(folder): path = os.path.join(folder, f) if os.path.isfile(path): if now - os.path.getmtime(path) > MAX_AGE: os.remove(path)