Last active
July 25, 2017 16:30
-
-
Save preytaren/3983295d523f099615e5a5ff9233988f to your computer and use it in GitHub Desktop.
Helps you cleanup files in a specified folder, especially downloads. Re-group files in folders named by date it's created.
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
# -*- encoding= utf-8 | |
“”“ | |
“”” | |
import os | |
import sys | |
import re | |
import shutil | |
import time | |
from collections import defaultdict | |
dir_pattern = re.compile(r'[0-9]{4}-[0-9]{2}-[0-9]{2}') | |
def move_files(files, folder): | |
""" | |
move files into specified folder | |
""" | |
if not os.path.exists(folder): | |
os.mkdir(folder) | |
for file in files: | |
shutil.move(file,os.path.join(folder, file)) | |
def files_in_folder(folder): | |
for filename in os.listdir(folder): | |
yield filename, time.strftime('%Y-%m-%d', time.localtime(os.path.getatime(filename))) | |
def main(working_directory): | |
os.chdir(working_directory) | |
file_dict = defaultdict(list) | |
for file, atime in files_in_folder(os.path.abspath('.')): | |
if not (os.path.isdir(file) and dir_pattern.match(file)): | |
file_dict[atime].append(file) | |
for folder, files in file_dict.items(): | |
move_files(files, str(folder)) | |
if __name__ == '__main__': | |
if len(sys.argv) > 1: | |
working_directory = os.path.abspath(sys.argv[1]) | |
else: | |
working_directory = os.path.abspath(os.path.join(os.environ['home'],'Downloads')) | |
main(working_directory) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank for your snippet.
Test Platform:
Python361
Windows 7