For each file in the directory, the script checks its extension by using os.path.splitext(filename)[1] to extract the extension.
This month, I will show you how to make a nice and simple document sorting script in Python (and not just because my desktop is so messy that I’ve lost track of what’s even on there anymore). I’ve broken this script down into 4 steps below:
Research and Import the required libraries for the task.Check every file in the directory for the extension (e.g .doc, .xml, .jpg etc).Create folders for each file types (e.g create a ‘Images’ folder for .jpg files).- Move all the files types to the correct folders (e.g all .jpg files to ‘images’).
Step 4: Moving Files to Respective Folders
It then compares the extension against each category and moves the file to the corresponding folder if there is a match. The script iterates through the folders dictionary using a loop.
For each category, it checks if the file’s extension (converted to lowercase) is present in the list of extensions for that category. If there is a match, it creates the target folder if it doesn’t exist already using os.makedirs(target_folder) and moves the file from the original location to the target folder using shutil.move().
Once the organize_files function is defined, the script provides the directory_path variable, representing the path to the directory containing the files to be organized. Finally, the organize_files(directory_path) function is called to initiate the file organization process. Upon completion, a confirmation message, “File organization complete,” is displayed.
Lets see it all put together!
#Import OS for operating system interaction.
import os
#Import Shell Utilities for high level file operations.
import shutil
def organize_files(directory):
#Assigned folders for different file categories.
folders = {
"Excel Spreadsheets": [".xls", ".xlsx", ".csv"],
"Images": [".jpg", ".jpeg", ".png", ".gif", ".bmp"],
"Text Files": [".txt"],
"Word Documents": [".doc", ".docx"]
#To add more: "Folder Name": [".extension"]. Remember to add/remove the ',' as required.
}
#Go through every file in the specifed directory.
for filename in os.listdir(directory):
if os.path.isfile(os.path.join(directory, filename)):
#Get the file extension
extension = os.path.splitext(filename)[1]
#Check the extension against each category above and move the file.
for folder, extensions in folders.items():
if extension.lower() in extensions:
target_folder = os.path.join(directory, folder)
if not os.path.exists(target_folder):
os.makedirs(target_folder)
shutil.move(os.path.join(directory, filename), os.path.join(target_folder, filename))
#Provide the directory path where the files are located.
directory_path = "C:/file-organiser-script/test-data"
#Call the function to organize the files.
organize_files(directory_path)
#A nice little confirmation message for when it's completed.
print("File organization complete.")
Stay tuned next month for when try to fix more of the consequences of my own actions.
“The code you write makes you a programmer. The code you delete makes you a good one. The code you don’t have to write makes you a great one.” – Mario Fusco.