Skip to content

Instantly share code, notes, and snippets.

@wbhinton
Created January 20, 2020 16:55
Show Gist options
  • Save wbhinton/4f2f6e557f829038a4ea57b88ee57146 to your computer and use it in GitHub Desktop.
Save wbhinton/4f2f6e557f829038a4ea57b88ee57146 to your computer and use it in GitHub Desktop.
File clean up
"""
-*- coding: utf-8 -*-
========================
Python Lazy Junk Files Organizer
========================
========================
"""
import os
from pathlib import Path
DIRECTORIES = {
"HTML": [".html5", ".html", ".htm", ".xhtml"],
"IMAGES": [".jpeg", ".jpg", ".tiff", ".gif", ".bmp", ".png", ".bpg", ".svg",
".heif", ".psd",".tif",".tiff"],
"VIDEOS": [".avi", ".flv", ".wmv", ".mov", ".mp4", ".webm", ".vob", ".mng",
".qt", ".mpg", ".mpeg", ".3gp"],
"DATA":[".csv",".dat",".json",".db"],
"DOCUMENTS": [".oxps", ".pages", ".docx", ".doc", ".fdf", ".ods",
".odt", ".pwi", ".xsn", ".xps", ".dotx", ".docm", ".dox",
".rvg", ".rtf", ".rtfd", ".wpd", ".xls", ".xlsx",".xlsxm", ".ppt",
".pptx",".odf",".odp",".odg",".pps"],
"EBOOKS":[".epub",".mobi",".djvu",".awz",".awz3"],
"ARCHIVES": [".a", ".ar", ".cpio", ".iso", ".tar", ".gz", ".rz", ".7z",
".dmg", ".rar", ".xar", ".zip",".bak",".tar.gz",".deb"],
"AUDIO": [".aac", ".aa", ".aac", ".dvf", ".m4a", ".m4b", ".m4p", ".mp3",
".msv", ".ogg", ".oga", ".raw", ".vox", ".wav", ".wma"],
"PLAINTEXT": [".txt", ".in", ".out"],
"FONTS":[".ttf",".fnt",".fon",".otf"],
"PDF": [".pdf"],
"POWERBI":[".pbix"],
"PYTHON": [".py",".ipynb"],
"IMAGES":[".iso",".img"],
"XML": [".xml"],
"EXE": [".exe"],
"SQL":[".sql"],
"SHELL": [".sh"],
"C++": [".cpp"],
"C": [".c"],
"ASP Classic": [".asp"],
"ASP_NET": [".aspx", ".axd", ".asx", ".asmx", ".ashx"],
"CSS": [".css"],
"Coldfusion": [".cfm"],
"Erlang": [".yaws"],
"Flash": [".swf"],
"Java": [".jsp", ".jspx", ".wss", ".do", ".action"],
"JavaScript": [".js"],
"Perl": [".pl"],
"PHP": [".php", ".php4", ".php3", ".phtml"],
"Ruby": [".rb", ".rhtml"],
"SSI": [".shtml"],
"LaTeX":[".tex"],
"XML": [".xml", ".rss", ".svg"]
}
FILE_FORMATS = {file_format: directory
for directory, file_formats in DIRECTORIES.items()
for file_format in file_formats}
def organize_junk():
for entry in os.scandir():
if entry.is_dir():
continue
file_path = Path(entry)
file_format = file_path.suffix.lower()
if file_format in FILE_FORMATS:
directory_path = Path(FILE_FORMATS[file_format])
directory_path.mkdir(exist_ok=True)
file_path.rename(directory_path.joinpath(file_path))
try:
os.mkdir("OTHER-FILES")
except:
pass
for dir in os.scandir():
try:
if dir.is_dir():
os.rmdir(dir)
else:
os.rename(os.getcwd() + '/' + str(Path(dir)), os.getcwd() + '/OTHER-FILES/' + str(Path(dir)))
except:
pass
if __name__ == "__main__":
organize_junk()
@grijalva10
Copy link

Thanks for sharing.

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