Sort and store files with same extension in Python
Suppose, you have a folder with hundreds of files that are not managed properly. Hence, creating a mess and now you want to arrange them in different folders. So, to store files with the same extension you just need this Python program. Then it will do your work in seconds and you are good to go.
For example, I have this folder having 111 files of different extensions and I want to arrange them in folders according to there extensions. So that files with the same extensions will be present in the same folder.
Modules Required inside the program
We need to use some Python modules like os and shutil, using these we can easily sort and store files with same extension using Python program. These modules can easily be imported into our Python program using the import command.
- OS Module is used here to change the directories and check for the existence of another directory in the current directory using os.path.exists() command in which the path of the directory is given.
This module is also being used to split the file name and extension into different variables as per our requirement using the os.path.splitext() command in which the name of the file is given.
This module is also being used to make a list of all files using the os.listdir() command in which the name of the directory is given.
- Shutil Module is used here to move files from one directory to another using shutil.move() command. The first attribute refers to the current location of the file and the second attribute refers to the future location of the file with the file name also i.e, where the file to be moved and the name of the file is to be specified as the second attribute.
Program Functioning for storing and sorting files with same extension in Python
I already discussed the modules used in this program which covers most of the explanation. Now, comes the remaining explanation of the program as follows:
- The input() command is used to take the directory name from the user. The directory should always be present in the same directory in which you have your Python program.
- The for loop is used to iterate through the list of file names stored in list li. This is the most important part of our program as all steps of moving files are done in this part.
- extension = extension[1:], this simply slice down the extension part having no dots(.) in it. For Example, the extension is (.jpg) but we just need (jpg), that’s what this line of code does for us.
- Here, the if statement is used to check whether any extension exists or not. If no, then continue is used to check for the next file but if yes, then simply move towards the next line of code.
- Then, the next if-else statement is used here to check whether the directory for an extension already exists or not. If yes, then just move the file to that directory, and if no, then make one and move that file to that newly-created directory.
import os import shutil dirName = input( 'Enter folder name: ' ) li = os.listdir(dirName) for i in li: fileName, extension = os.path.splitext(i) extension = extension[1:] if extension == "": continue if os.path.exists( dirName + '/' + extension ): shutil.move( dirName + '/' + i, dirName + '/' + extension + '/' + i ) else: os.makedirs( dirName + '/' + extension ) shutil.move( dirName + '/' + i, dirName + '/' + extension + '/' + i )
Enter folder name: Files
Here, you can see that I have now all files with the same extensions are moved to the different folders and folder names are set to their extension name.