Last active
March 25, 2019 12:45
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!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