Skip to content

Instantly share code, notes, and snippets.

@preytaren
Last active July 25, 2017 16:30
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save preytaren/3983295d523f099615e5a5ff9233988f to your computer and use it in GitHub Desktop.
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.
# -*- 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)
@vereperrot
Copy link

  • Remove the line2-4. It will occour an error "SyntaxError: invalid character in identifier".
  • You have to replace the home variable as "HOMEPATH". Then it works in the windows 7.

Thank for your snippet.
Test Platform:
Python361
Windows 7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment