Javatpoint Logo
Javatpoint Logo

File Organizer: Write a Python program that organizes the file in a directory based on the extension

The general process to construct a Python program that organizes files in a directory is as follows:

1. Identify the Directory - You must find the source directory to be organized. It is important to take note of the files contained in that directory. The files could be of different types or a particular type.

File Organizer: Write a Python program that organizes the file in a directory based on the extension
File Organizer: Write a Python program that organizes the file in a directory based on the extension

Following are the snapshots of the source directory before organizing the directory.

Define the path of the source directory containing the file to be organized:

For example:

In our program, we have taken input from the user for the source directory.

2. Define the Category - It is important to define the categories you want to organize based on the types of files stored in the directory. For example, if you wish to organize the directory containing images. You might want to categorize them based on the year, month, date, image type, location where they were taken, etc.

This is how you can define the categories of the files:

3. Define the path of the Destination - In some cases, you might want to move the files to different locations. For that, you must define the destination path.

For Example:

The paths of the source and destination directories are the same. So, the program will organize the files in the source directory.

In our program, we have also taken input from the user for the destination directory. Also

4. Write code to sort files - Write a code to sort the files based on the chosen category in the directory. You must read the files in the directory, extract the relevant metadata (such as the creation date or file type), and then move the files to the correct category directory using Python's built-in functions.

Following is the code that sorts the files based on the type of the file and moves all the file to their respective directory.

5. Test Your Code - Once the program has been developed, test it on a small subset of files to ensure it functions as intended. If there are any problems, you can fix the code and test it once more to ensure it is functioning properly. Otherwise, you might lose your data.

6. Run the code on the source directory - Now, you are ready to arrange all of the files in the directory. For that, you can execute the code on the source directory. Before running the code, it's crucial to make sure that you have a backup of the files in case something goes wrong.



Enter the source directory path : C:\Users\DELL\Desktop\Temp
Enter the destination directory path : C:\Users\DELL\Desktop\Temp

File Organizer: Write a Python program that organizes the file in a directory based on the extension

The program has successfully organized the files in the directory.


  • The os and shutil modules are imported to perform file system operations and move files between directories.
  • The source_dir and dest_dir variables are defined using the input() function to get user input for the source and destination directories. The .replace('\\', '/') method is used to replace backslashes with forward slashes in the file paths to ensure compatibility across different operating systems.
  • The extension_map dictionary is defined to map file extensions to their respective folders. Each file extension is key in the dictionary, and its value is the folder name where files with that extension should be moved.
  • The set() function is used to get a set of unique folder names from the values of the extension_map
  • A for loop is then used to iterate over the set of folder names and create each folder in the destination directory using os.makedirs(). The exist_ok=True argument ensures that the function does not raise an error if the folder already exists.
  • Another for loop is used to iterate over each file in the source directory using listdir(). For each file, the os.path.splitext() function is used to get the file extension in lowercase. We then check if the extension is in the extension_map. If it is, we use shutil.move() to move the file to the appropriate folder in the destination directory.

Note that this is just an example code; you may need to modify it to suit your specific needs. You should also be careful when running code that moves or modifies files, as there is a risk of data loss if something goes wrong.


Some improvements that can be made to the given program are as follow:

  1. Error Handling - It would be advantageous to include error handling when dealing with situations where the user provides incorrect or invalid paths. This can include checking whether the directories are present and reachable, and the user will receive the proper error messages or prompts to correct their input.
  2. Flexibility in Extension Mapping - Although it might be sufficient for the current use case, the program's predefined dictionary extension map might not include all potential file types. Make the extension mapping more adaptable by giving users the option to create their own mappings or by implementing a more reliable method to recognize file kinds automatically.
  3. Logging and Reporting - Adding logging and reporting features can be beneficial for program debugging and troubleshooting. You can implement logging statements or a logging library to record any errors, warnings, or information occurring while the application is running.
  4. Documentation - Clear and thorough documentation must be provided to ensure that others understand and use the program correctly. In addition to updating the file with usage guidelines, examples, and any other relevant data, consider comments to the code to describe the purpose and functioning of certain functions.

Remember that these are only recommendations for possible future improvements; you can prioritize and implement them according to your own needs and specifications.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA