Create your own file organiser in Python
Tags: python, tutorial
Published: 2018-11-14 11:01:42

---
title: Create your own file organiser in Python
published: true
description: Tutorial to create your own file organiser in Python
tags: python, tutorial 
---
In macOS Mojave, there is a feature called "Use Stacks" which organise your desktop files.
<img width="250" alt="1" src="https://user-images.githubusercontent.com/20112458/48477125-c36e7f00-e826-11e8-855e-80ac048ce39b.png">

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`

```sh
#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()
```
To run this program open terminal and write
    
    $ python organise.py
My desktop before running this program:
<img width="494" alt="2" src="https://user-images.githubusercontent.com/20112458/48477126-c49fac00-e826-11e8-96eb-20babcc0d7c6.png">
<br>
After running this program:
<img width="491" alt="screenshot 2018-11-14 at 4 28 26 pm" src="https://user-images.githubusercontent.com/20112458/48478450-55c45200-e82a-11e8-97a6-4e5d2c8b0ef4.png">