DEV Community

Sahil Rajput
Sahil Rajput

Posted on

Create your own file organiser in Python

In macOS Mojave, there is a feature called "Use Stacks" which organise your desktop files.
1

I love this feature but you can only use this feature to organise files on your desktop and not in every folder.
So, I decided to make a python program which will organise my files in any folder.

Create a file organise.py

#import libraries
import os
from pathlib import Path
#Dictionary (Add more if you want)
DIRECTORIES = {
    "HTML": [".html5", ".html", ".htm", ".xhtml"],
    "IMAGES": [".jpeg", ".jpg", ".tiff", ".gif", ".bmp", ".png", ".bpg", "svg",
               ".heif", ".psd"],
    "VIDEOS": [".avi", ".flv", ".wmv", ".mov", ".mp4", ".webm", ".vob", ".mng",
               ".qt", ".mpg", ".mpeg", ".3gp", ".mkv"],
    "DOCUMENTS": [".oxps", ".epub", ".pages", ".docx", ".doc", ".fdf", ".ods",
                  ".odt", ".pwi", ".xsn", ".xps", ".dotx", ".docm", ".dox",
                  ".rvg", ".rtf", ".rtfd", ".wpd", ".xls", ".xlsx", ".ppt",
                  "pptx"],
    "ARCHIVES": [".a", ".ar", ".cpio", ".iso", ".tar", ".gz", ".rz", ".7z",
                 ".dmg", ".rar", ".xar", ".zip"],
    "AUDIO": [".aac", ".aa", ".aac", ".dvf", ".m4a", ".m4b", ".m4p", ".mp3",
              ".msv", "ogg", "oga", ".raw", ".vox", ".wav", ".wma"],
    "PLAINTEXT": [".txt", ".in", ".out"],
    "PDF": [".pdf"],
    "PYTHON": [".py"],
    "XML": [".xml"],
    "EXE": [".exe"],
    "SHELL": [".sh"]
}

FILE_FORMATS = {file_format: directory
                for directory, file_formats in DIRECTORIES.items()
                for file_format in file_formats}
#This will organise your files
def organize():
    for entry in os.scandir():
        if entry.is_dir():
            continue
        file_path = Path(entry.name)
        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))
   #if extension not present in the dctionary than create a folder name "OTHER"
    try:
        os.mkdir("OTHER")
    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/' + str(Path(dir)))
        except:
            pass
if __name__ == "__main__":
    organize()
Enter fullscreen mode Exit fullscreen mode

To run this program open terminal and write

$ python organise.py
Enter fullscreen mode Exit fullscreen mode

My desktop before running this program:
2


After running this program:
screenshot 2018-11-14 at 4 28 26 pm

Top comments (5)

Collapse
 
joehobot profile image
Joe Hobot •

I never use desktop for anything to store, I have 0 icons.. Just because I have this tendency to hide work and personal sh** when doing presentations or videos.. so my stuff is in some off dirs that I create when getting new Mac.

Tbh I've seen some weird desktop stuff on peoples Macs when they do presentations.

Few that I remember:
Divorce Papers
Odd Images (nothing NSFW, but just bizarre and weird)
Xyz Company Clients
Resumes
Taxes

Went bit off topic, but nice script tho, am Python Follower myself :)

Collapse
 
presto412 profile image
Priyansh Jain •

My teacher accidentally showed us an explicit video thumbnail once while trying to locate his lecture slides haha

Collapse
 
menjilx profile image
Menj •

How about adding a folder then auto set a password for that folder?😊 thanks for sharing?

Collapse
 
themainframech profile image
The Mainframe •

Cool! Thanks for sharing :)

Collapse
 
sahilrajput profile image
Sahil Rajput •

Welcome :)