Last active
January 18, 2018 15:06
-
-
Save sleepless-se/0cee31ec35c8a270e23b182bd001ae81 to your computer and use it in GitHub Desktop.
Clean your mac of desktop and download folder.
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
import os | |
import datetime | |
import shutil | |
desktop_path = os.environ['HOME'] + "/Desktop/" | |
downloads_path = os.environ['HOME'] + "/Downloads/" | |
days = 7 | |
oldF_folder = "old/" | |
def moveOldFiles(current_path,oldFolder,dasy): | |
print(current_path) | |
dir = os.listdir(current_path) | |
old_folder = os.path.join(current_path ,oldFolder) | |
if(os.path.exists(old_folder)): | |
None | |
else: | |
os.mkdir(old_folder) | |
today = datetime.date.today() | |
minus = datetime.timedelta(days=days) | |
deadline = today - minus | |
print(deadline) | |
for file_name in dir: | |
print(file_name) | |
target = current_path+file_name | |
print(target) | |
if os.path.exists(target): | |
dt = datetime.datetime.fromtimestamp(os.stat(target).st_mtime) | |
key = dt.strftime('%Y/%m/%d %H:%M:%S') | |
print(str(key) + " - " + str(dt)) | |
if(dt.date()<deadline): | |
try: | |
shutil.move(target,old_folder) | |
except Exception as e: | |
print(e) | |
print("moved") | |
else: | |
print("keep") | |
else: | |
print("Can't open " + file_name) | |
print("") | |
if __name__ == '__main__': | |
moveOldFiles(downloads_path,oldF_folder,days) | |
moveOldFiles(desktop_path,oldF_folder,days) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment