Last active
January 21, 2018 05:27
-
-
Save pranavsricharan/33d88f3a2789aacd998b9c8a9041ef20 to your computer and use it in GitHub Desktop.
Organize Downloads 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
""" | |
Author: Pranav Sricharan | |
Github: @pranavsricharan | |
Description: | |
*** Organize your downloads folder by categorizing your files | |
*** The script runs once in every 2 hours (See TIME_INTERVAL) | |
*** Works only for files and not directories | |
*** Not the most efficient code but it works | |
""" | |
import os | |
import time | |
from shutil import move | |
USER_DIR = os.path.expanduser('~') | |
PATH = os.path.join(USER_DIR, 'Downloads') # Path to Downloads folder | |
FILE_EXT_MAP = { | |
'Images': ['jpg', 'jpeg', 'png'], | |
'Audios': ['mp3', 'm4a', 'aac', 'wav'], | |
'Videos': ['avi', 'mp4', 'wmv', 'mkv', 'flv'], | |
'Archives': ['zip', 'rar', 'gz', 'tar', '7z'], | |
'Documents': ['doc', 'docx', 'pdf'], | |
'Other documents': ['ppt', 'pptx', 'xls', 'xlsx'], | |
'GIFs': ['gif'], | |
'Applications': ['exe', 'msi', 'deb'], | |
'Torrents': ['torrent'] | |
} | |
TIME_INTERVAL = 7200 | |
def move_file(file): | |
file_ext = file[file.rfind('.') + 1:] # Find file extension | |
output_dir = None | |
# Find the corresponding directory | |
for key in FILE_EXT_MAP: | |
if file_ext in FILE_EXT_MAP[key]: | |
output_dir = key | |
break | |
# Move file if it can be categorized, else ignore | |
if output_dir is not None: | |
path = os.path.join(PATH, output_dir) | |
os.makedirs(path, exist_ok=True) # Create directory if not exists | |
try: | |
move(file, os.path.join(path, os.path.basename(file))) | |
except: | |
pass | |
if __name__ == '__main__': | |
while True: | |
dir_contents = os.listdir(PATH) | |
for item in dir_contents: | |
full_path = os.path.join(PATH,item) | |
if os.path.isfile(full_path): | |
move_file(full_path) | |
time.sleep(TIME_INTERVAL) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment