You are currently viewing This Script Will Take the Pain Away From File Organization

This Script Will Take the Pain Away From File Organization

In today’s digital age, it’s common to have a large number of files on our computers, and keeping them organized can be a time-consuming task. Whether it’s sorting through images, documents, or music files, organizing them into appropriate folders can be a tedious and repetitive process. But what if there was a way to automate this task?

Well, the good news is that there is! With the help of a simple Python script, you can easily automate the process of organizing your files. In this blog post, we will walk you through the process of creating a script that will sort your files based on their extensions and move them to the appropriate folders.

Importing the necessary modules…

First, let’s start by importing the necessary modules. In this script, we will be using two modules – ‘os’ and ‘shutil’. The ‘os’ module provides a way to interact with the operating system, and the ‘shutil’ module provides functions for file operations, such as moving files from one location to another. To import these modules, add the following lines of code at the beginning of your script:

import os
import shutil

Taking care of organizing the files…

Next, we will define a function called ‘organize_files’ that will take care of organizing the files. This function will have two parameters – ‘src_folder’ and ‘dst_folder’. ‘src_folder’ is the source folder where the files to be organized are located, and ‘dst_folder’ is the destination folder where the organized files will be moved. Here’s the code for the ‘organize_files’ function:

def organize_files(src_folder, dst_folder):
    Organizes files in src_folder and moves them to dst_folder based on file extension.
    # List all files in the source folder
    files = os.listdir(src_folder)

    # Iterate over the list of files
    for file in files:
        # Get the file extension
        file_extension = file.split(".")[-1]

        # Create a destination folder for the file extension
        extension_folder = os.path.join(dst_folder, file_extension)
        os.makedirs(extension_folder, exist_ok=True)

        # Move the file to the destination folder
        shutil.move(os.path.join(src_folder, file), os.path.join(extension_folder, file))

Let’s go through each piece of code in the ‘organize_files’ function step by step:

  1. The first step is to get a list of all the files in the ‘src_folder’. This is done using the ‘os.listdir’ function. The list of files is stored in the ‘files’ variable.
  2. We then use a for loop to iterate over the list of files in the ‘files’ variable. For each file in the list, the next steps are performed.
  3. The file extension is obtained by splitting the file name on the ‘.’ character and taking the last element in the resulting list. This will give us the file extension, which is stored in the ‘file_extension’ variable.
  4. The destination folder for the file extension is created using the ‘os.path.join’ function to concatenate the ‘dst_folder’ and the ‘file_extension’ variables. The ‘os.makedirs’ function is used to create the folder if it doesn’t already exist. The ‘exist_ok’ argument is set to ‘True’ to avoid an error if the folder already exists.
  5. Finally, the ‘shutil.move’ function is used to move the file from the ‘src_folder’ to the ‘dst_folder/file_extension’ location. The ‘os.path.join’ function is used to concatenate the file’s path in the ‘src_folder’ and the destination path in the ‘dst_folder/file_extension’ folder.

Call it in…

With the ‘organize_files’ function defined, all that’s left is to call it and pass in the ‘src_folder’ and ‘dst_folder’ as arguments. Here’s an example of how to call the function:

if __name__ == '__main__':
    src_folder = '/path/to/src_folder'
    dst_folder = '/path/to/dst_folder'
    organize_files(src_folder, dst_folder)

In this example, the ‘src_folder’ is set to ‘/path/to/src_folder’ and the ‘dst_folder’ is set to ‘/path/to/dst_folder’. Replace these with the actual paths to your source and destination folders. The code inside the ‘if name == ‘main’:’ block will only be executed when the script is run directly, and not when it’s imported as a module.

And that’s it! You now have a simple script that can automate the task of organizing your files. You can customize the script to fit your needs by adding more file extensions, changing the folder names, or modifying the organization scheme.


In conclusion, automating repetitive tasks like file organization can save you a lot of time and effort. With just a few lines of code, you can create a script that will do the work for you. Python is a powerful and flexible language, and with the help of modules like ‘os’ and ‘shutil’, you can easily automate tasks like file organization. So, why not give it a try and see how much time you can save?


  • What is the purpose of this script?
    The purpose of this script is to automate the task of organizing files based on their extension. The script moves all the files in a source folder to separate folders based on their extension, making it easier to manage and find your files.
  • What are the prerequisites for using this script?
    You will need to have Python installed on your computer to run this script. Additionally, you will need to have the ‘os’ and ‘shutil’ modules installed, which are part of the standard library.
  • What are the ‘src_folder’ and ‘dst_folder’ in the script?
    The ‘src_folder’ is the location of the folder where your files are stored. The ‘dst_folder’ is the location where you want to store the organized files.
  • Can I add more file extensions to the script?
    Yes, you can add more file extensions to the script by adding more entries to the ‘extensions’ dictionary. Simply add a new key-value pair where the key is the file extension and the value is the destination folder.
  • Can I change the organization scheme of the script?
    Yes, you can change the organization scheme by modifying the ‘organize_files’ function. You can change the names of the destination folders, add or remove file extensions, or modify the way files are moved to their destination folders.
  • How do I run the script?
    To run the script, simply save the code to a file with a .py extension and run it from the command line or terminal. You can also run the script from the Python interpreter by importing the file as a module.
  • How does the script handle files that already exist in the destination folders?
    The script will overwrite any files with the same name in the destination folders. If you want to keep both the original and the newly organized files, you will need to modify the script to add a unique identifier, such as a timestamp, to the file name.
  • Can I use this script on a different operating system besides Windows?
    Yes, this script will work on any operating system that supports Python, including macOS and Linux. However, you may need to modify the paths to the source and destination folders to match the format used by your operating system.

Leave a Reply