Skip to content

Instantly share code, notes, and snippets.

@caiiiycuk
Created August 23, 2018 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caiiiycuk/6d90e24b0d6e05487a73d47169f040f1 to your computer and use it in GitHub Desktop.
Save caiiiycuk/6d90e24b0d6e05487a73d47169f040f1 to your computer and use it in GitHub Desktop.
Python 2, 3: Find and delete files last modified over than year ago
import os
import time
import sys
src = os.getcwd()
oneyear = 365 * 24 * 60 * 60
totalsaved = 0
with open('old_files.sh', 'w') as f:
for root, dirs, files in os.walk(src):
for file in files:
abspath = os.path.join(root, file)
try:
stat = os.stat(abspath)
except:
print("Can't stat %s: %s" % (abspath, sys.exc_info()[0]))
continue
timesec = os.stat(abspath).st_atime
todelete = time.time() - timesec > oneyear
if todelete:
totalsaved += stat.st_size
f.write('rm -v "' + abspath + '"\n')
print('Total saved %.2f Mb, rm -v %s' % (totalsaved / 1024 / 1024, abspath))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment