Created
August 23, 2018 21:35
-
-
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
This file contains hidden or 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
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