Skip to content

Instantly share code, notes, and snippets.

@smartm13
Last active March 25, 2019 12:45
Show Gist options
  • Save smartm13/44b6c41bbf39a5b4aa6de80696392b29 to your computer and use it in GitHub Desktop.
Save smartm13/44b6c41bbf39a5b4aa6de80696392b29 to your computer and use it in GitHub Desktop.
UNTESTED. python3 script to delete all files OLDER than minAge_sec inside basepath (recursively)
#!python3
#script to delete all files OLDER than minAge_sec inside basepath (recursively)
#set these args before running
basepath="."
minAge_sec=14*24*60*60 #14days
logfilepath="~/deletionJob.log"
#ofcourse, logfilepath should lie outside basepath
import os,time
def logger(msg,logfile=logfilepath):
with open(logfile,'a') as f:
f.write(msg+'\n')
logger("DeletionJob started at : {}".format(time.time()))
for root,dirs,files in os.walk(basepath):
for file in files:
target=os.path.join(root,file)
ctime=os.stat(target).st_ctime #ctime for create date,mtime for modified
age=time.time()-ctime
if age>minAge_sec:
try:
#delete target file
os.remove(target)
#log it to other file
logger('Deleted file "{}" create-time:{}'.format(target,ctime))
except Exception as e:
logger('Failed removal of "{}" reason:{}'.format(target,e))
logger("DeletionJob completed at : {}".format(time.time()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment