Python File Organizer

2020-03-29 Uttkarsh Kabde Download Code

How to create file organizer in python?

Hey Programmers, we are back with new topic. If your desktop or any folder looks messy then this post is for you. Finding a file is always becomes hard for me as I have a bad habit of storing all data in either one folder or on desktop. And every time I go to find a particular file I told myself to sort this mess and never do it again. But it happens only in my mind not on system. So I decided to write a program that will automatically sort all the files and keep them according to their media type. So here is the code that I was talking about. Then without wasting time we must move to our program. So write and run this program we need few libraries. But luckily libraries that we need already installed with python installation. so instead of installing any other libraries we move to coding section.

#importing the required libraries

import os
from pathlib import Path

# Defining the available and common extensions in directories named with basic file types

    "HTML": [".html5", ".html", ".htm", ".xhtml"],
    "IMAGES": [".jpeg", ".jpg", ".tiff", ".gif", ".bmp", ".png", ".bpg", "svg", ".psd"],
    "VIDEOS": [".avi", ".flv", ".wmv", ".mov", ".mp4", ".webm", ".mpg", ".mpeg", ".3gp", ".mkv"],
    "DOCUMENTS": [".oxps", ".epub", ".pages", ".docx", ".doc",".ods",".odt", ".rtf", ".rtfd",  ".xls", ".xlsx", ".ppt",".pptx"],
    "ARCHIVES": [ ".iso", ".tar", ".gz",  ".7z", ".rar", ".xar", ".zip"],
    "AUDIO": [".aac",  ".m4a",  ".mp3", "ogg", "oga", ".raw", ".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}

Actual code that will sort all the files and move to their file type folder

#This section will sort files

def organize_file():
    for entry in os.scandir():
        if entry.is_dir():
        file_path = Path(
        file_format = file_path.suffix.lower()
        if file_format in FILE_FORMATS:
            directory_path = Path(FILE_FORMATS[file_format])

If there is (or are )few files with different extensions that are not defined in directories then don’t worry. program is smart enough to sort them too. Our program will keep those type of files to another folder and folder will be named as “OTHER”. So you can find them easily.

   #if extension is not given in the dctionary than program will automatically  creates a folder name "OTHER" and will store those files into this

    for dir in os.scandir():
            if dir.is_dir():
                os.rename(os.getcwd() + '/' + str(Path(dir)), os.getcwd() + '/OTHER/' + str(Path(dir)))

Now we are going to call main function that will execute our whole program

if __name__ == "__main__":


All gather together you can find this complete code with the file named in download section. Now you have to just download the file and keep in the folder or local directory where you want to sort all the mess you have created this long.

After putting this file at desired location open terminal or cmd into that location and run the file with command


It will take time and when it done execution you will be surprised as I am. That’s all. Here I am showing my mess that I have made before executing the code. And the sorted files after execution.

Before Running The Code:

Mess in Folder before running this Code

After Running The Code:

after running the organize_file code


So Here we stop for now. We will be back with new topic. To get notified with our new topic do subscribe to our newsletter and join our social media accounts.

If you face and problem regarding this code feel free to comment in below comment section. we will try to solve your problem as soon as possible

Happy Coding..

About author

Card image cap
Uttkarsh Kabde

Currently I am Persuing Mtech in IT at Shri Guru Gobind Singh Institute of Engineering and Technology, Nanded!

-Web App Developer


Scroll to Top