Created
August 10, 2015 20:23
-
-
Save xcombelle/f2025540089e3902bb63 to your computer and use it in GitHub Desktop.
reddit improvements on https://www.reddit.com/r/learnpython/comments/3ge24x/organizing_script_complete/
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
from __future__ import print_function #useless in python3 | |
import os, time, shutil | |
import os.path | |
from glob import glob | |
import datetime | |
###Loging### | |
def logging(message): | |
time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
log_name = "log.txt" | |
if os.path.isfile(log_name): | |
log = open(log_name, 'a') | |
else: | |
log = open(log_name, 'w') | |
print('New "{logname}" created at {time} \n'.format(logname= log_name, time=time),file=log) | |
print("{message} at {time}" .format (message=message, time=time),file=log) | |
log.close() | |
###Categories### | |
categories_dict = {"Audio":["mp3"], | |
"Compressed":["zip", "rar", "7z", "tar"], | |
"Disk image":["img", "iso"], | |
"Document":["doc", "pdf", "xls", "xlsx", "docx", "reg", "bat"], | |
"Folders":[], | |
"Image":["png", "jpeg", "jpg", "psd", "JPG", "JPEG"], | |
"Program":["msi", "exe", "apk"], | |
"Video":["webm","mp4", "wmv", "gif", "xspf"], | |
"Code":["py", "pyc", "bat", "pyw"]} | |
categories = list(categories) | |
###Folders and Cataloging### | |
def create_folders(): | |
for cat in categories: | |
if os.path.exists(cat) == False: | |
os.mkdir(cat) | |
logging('Folder "{category}" created'.format(cat)) | |
files_dic = {} | |
def file_types(): | |
files = glob("*") | |
for file_ in files: | |
if os.path.isdir(file_): | |
files_dic[file_] = "folder" | |
else: | |
files_dic[file_] = "file" | |
###Mover### | |
def mover(file_, category): | |
black_list = [os.path.basename(__file__), "log.txt"] | |
if file_ not in black_list: | |
try: | |
shutil.move(file_, category) | |
logging('"{file}" was moved to "{category}"' .format (file=file_, category=category)) | |
except: | |
logging('"{file}" already exists in "category", it was removed'.format(file=file_, category=category)) | |
os.remove(file_) | |
pass | |
###Main### | |
def main(file_dict, categories, categories_dict): | |
for cat in categories: | |
if cat != "Folders": | |
for ext in categories_dict[cat]: | |
files = glob("*.{ext}".format(ext=ext)) | |
for file_name in files: | |
mover(file_name, cat) | |
else: | |
for file_name in file_dict: | |
if file_dict[file_name] == "folder" and file_name not in categories: | |
mover(file_name, "Folders") | |
###Run### | |
if __name__ == '__main__': | |
create_folders() | |
file_types() | |
main(files_dic, categories, categories_dict) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment