DEV Community

Cover image for How to make Python Script that organises files

Posted on

How to make Python Script that organises files

In this tutorial, you'll learn how to create a Python script that organises your folders by file extension.

The prerequisites of this article is that you should be comfortable with Basic Python programming and how to use modules in Python.

The downloads folder in my PC is second Recycle Bin to me.
Here is what it looks like.

There are 112 files in that folder, and I am not very good at moving each file one by one after validating the file type. Let's use Python to automate the task.

What to build?

A script or GUI programme that asks for the folder that needs to be organised and then moves all of the files according to their file types. For instance, if I have files like these:

Enter fullscreen mode Exit fullscreen mode

After running the script the folder structure should be:

     |_ Documents/
     |     |_ presentation.pptx
     |     |_ project.docx
     |_ Code/
     |     |_ index.html
     |_ Images/
     |     |_ cover.jpeg
     |     |_ header.png
     |_ Others/
           |_ file.dll
Enter fullscreen mode Exit fullscreen mode

Looks so clean now. Each file type has its own dedicated folder, the lookup has become much easier.

The Planning

Before touching the keyboard first we must design our logic, here is what we need to do:

  • Ask for the folder that needs to be organized.
  • Retrieves all filepaths from the folder.
  • Check the filetype.
  • Move the file according to its extension.

Looks good to me, now lets code:

Step 1 : Tell File Extension to the script

First lets create a dictionary that tells our script the folder where each type of file will go.

filetypes = {
    "Images": ["png", "jpg", "jfif", "webp", "jpeg", "bmp", "tiff", "gif", "raw", "psd"],
    "Documents": ["doc", "docx", "ppt", "pptx", "xls", "xlsx", "pdf", "txt"],
    "Videos": ["mp4", "mpeg", "mkv", "srt"],
    "Code": ["html", "css", "js", "py", "cpp", "c"],
    "Audio": ["mp3", "wav", "ogg"],
    "Compressed": ["zip", "tar", "rar"],

Enter fullscreen mode Exit fullscreen mode

Step 2 : Ask the Directory Path

I am getting the Directory name using a GUI dialog box, that will be convenient. Tkinter has this amazing askdirectory function which we can use here.

from tkinter import filedialog
p = filedialog.askdirectory()
Enter fullscreen mode Exit fullscreen mode

When we run the above cade our dialog box is showing up and p contains the path of the directory which we selected. Okay so we are done with asking the directory name.

Step 3 : Getting all the files

Now we have to get all the files from the given directory, for this I am using Pathlib, a built-in module in Python that makes working with Paths super easy. But first we have to create a folder Organized like we planned above.

from Pathlib import Path
PATH = Path(p)
dest = PATH / "Organized"
Enter fullscreen mode Exit fullscreen mode

The above code take the path p provided by the tkinter file dialog box and sends it to the Path(), we make a new folder "Oganized" only if it is not already there, we do it by using exist_ok=True.

Now the iteration part,

files = []
for i in PATH.iterdir():
    if i.is_file():
Enter fullscreen mode Exit fullscreen mode

The above code iterates on every directory and files in the PATH, and if it is a file then saves it in the files list. (this is why we used Pathlib)

Step 3 : Check Type -> Move File

Now the only thing left to do is checking the file type then creating a folder for the file (not creating if it already exists) then move the file but move the file into Others folder if the file extension is unknown.

for file in files:
        # done flag tells that if the file matched to a dictionary value or not
        done = 0
        # iterate over the keys and check if the file belong to the particular key
        for k in filetypes.keys():
            # Check if the file extension is in the values of the key
            if file.suffix[1:] in filetypes[k]:
                done = 1
                # make a new folder with `key name` and move the file there
                destf = dest / f"{k}"
                shutil.move(str(file.resolve()), str(destf))

        if done != 1:
            # if the file was not present in the dictionary the make Others folder  and move the file there
            destf = dest / "Others"
            shutil.move(str(file.resolve()), str(destf))

Enter fullscreen mode Exit fullscreen mode

The above code take each file from the files list and then checks if the extension is present in the dictionary or not.
If the extension is in the dictionary, make a folder for the file type and move the file into that folder. If the file's extension isn't found in the folder, move it to the 'Others' folder.

Time to run the script : The moment of truth

I ran the script in my downloads folder, look how the script asked for the directory and within seconds created Organized folder which contains all the files.

Running Script

What more can be done?

The script is working fine but here are few things you can experiment with the script:

  • Use argparse for Command Line Input
  • Improve the performance, by removing the second loop and creating a new search method.
  • Show information about the files found and progress bar using tqdm

Try extending the script and make sure to create a PR on the repository.

Here is the script repository link

Thanks for sticking till the end, it means a lot.
If you liked the article then also consider checking me out on Twitter where I post stuff about python, javascript and open source daily.

Top comments (0)