An Actual Software an Absolute BEGINNER Can Make
Make a Simple File Sorting Software Using Python (A mini practice project for beginners)
In my experience, there is no better way to master any skill than to start using whatever knowledge you have gained to start solving practical problems. You'd be surprised at what the "little" you have learnt can achieve, for example, this file sorting software(a Python script) uses just the os
and shutil
modules of Python (more on this shortly) to sort and organise computer files.
In this article, I will be guiding you to create this simple file-sorting software using the following
Python programming language
os
Module: A Python module for interacting with a computer's operating system so that you can perform operations that involve file and directory manipulation. operations like listing files in a directory, checking file properties and navigating file paths.shutil
Module: This module works with theos
module to perform more high-level operations like copying, moving, deleting files and directories and handling file permissions.
What You'll Need
You need just two things:
Basic understanding of Python programming language ie python syntax and structures, variables and data types, and control structures like loops and conditionals.
Ensure you have Python installed on your computer and have a text editor or IDE such as Visual Studio Code, PyCharm or Jupyter Notebooks to write and execute your Python script.
Quick Overview
We will be coding a Python script that automatically sorts files into various folders according to their file types. it also creates folders for various file types if they do not exist. Users can also customise the directory and file types according to their needs. Making it much easier for users to stay organised.
Here is how well do it;
We'll begin by specifying the following: the target directory where our files are, the files we wish to sort and the folders we'll be sorting the files into.
Writing the code for creating folders if they don't exist.
Writing code to move the files into the appropriate directories and to handle exceptions if they come up.
Stage 1: Get Ready
Let's set the stage by importing and specifying everything we need for this project.
- First, we import the
os
andshutil
modules
#Importing the modules we'll be using
import os, shutil
- Next, we specify the source directory as path, filenames as file_names and folder names as folder_names
#Defining the source directory path,
#For this example it is set to desktop.
path = r"C:/Users/USER/Desktop/"
#Getting a list of all file names in the source directory
file_names = os.listdir(path)
#Defining the names of the target folders.
folder_names = ['csv_files', 'image_files', 'text_files', 'short_cuts', 'audio_files', 'ini_files', 'links', 'python_files', 'markdown_files', 'html_files', 'pdf_files']
Stage 2: Code The Sorting Algorithm
Now that we've specified the source directory, filenames and folders it's time to start moving files with code!
First, we write code to create target folders if they don't exist. This loops through each folder name in the folder names list and uses the
os.path.join
function to create a folder path by joining thepath
and folder name.# Create the target folders if they don't exist for folder_name in folder_names: folder_path = os.path.join(path, folder_name) if not os.path.exists(folder_path): os.makedirs(folder_path)
Next, we code the file sorting algorithm by iterating through the files in the source directory using if
statements and the os.path.exists
function to check if a file exists in the directory and the shutil.move
function to move the file from the source directory to the desired directory. see the code snippet below for an example moving csv files (files with the .csv)
extension to their appropriate folder.
# Move CSV files to the 'csv_files' folder
if '.csv' in file and not os.path.exists(os.path.join(path, 'csv_files', file)):
shutil.move(os.path.join(path, file), os.path.join(path, 'csv_files', file))
- Now let's repeat the process for all the other file types, changing the substring, and folder accordingly.
'csv_files', file)):
shutil.move(os.path.join(path, file), os.path.join(path, 'csv_files', file))
# Move MP3 files to the 'audio_files' folder
elif '.mp3' in file and not os.path.exists(os.path.join(path, 'audio_files', file)):
shutil.move(os.path.join(path, file), os.path.join(path, 'audio_files', file))
# Move DOCX files to the 'text_files' folder
elif '.docx' in file and not os.path.exists(os.path.join(path, 'text_files', file)):
shutil.move(os.path.join(path, file), os.path.join(path, 'text_files', file))
# Move INI files to the 'ini_files' folder
elif '.ini' in file and not os.path.exists(os.path.join(path, 'ini_files', file)):
shutil.move(os.path.join(path, file), os.path.join(path, 'ini_files', file))
# Move shortcut (LNK) files to the 'links' folder
elif '.lnk' in file:
source_file_path = os.path.join(path, file)
destination_file_path = os.path.join(path, 'links', file)
if not os.path.exists(destination_file_path):
try:
shutil.move(source_file_path, destination_file_path)
print(f"Moved '{file}' to the 'links' folder.")
except Exception as e:
print(f"Error moving '{file}': {e}")
else:
print(f"'{file}' already exists in the 'links' folder.")
# Move Python (PY) files to the 'python_files' folder
elif '.py' in file and not os.path.exists(os.path.join(path, 'python_files', file)):
shutil.move(os.path.join(path, file), os.path.join(path, 'python_files', file))
# Move PNG files to the 'image_files' folder
elif '.png' in file and not os.path.exists(os.path.join(path, 'image_files', file)):
shutil.move(os.path.join(path, file), os.path.join(path, 'image_files', file))
# Move R Markdown (RMD) files to the 'markdown_files' folder
elif '.rmd' in file and not os.path exists(os.path.join(path, 'markdown_files', file)):
shutil.move(os.path.join(path, file), os.path.join(path, 'markdown_files', file))
# Move HTML files to the 'html_files' folder
elif '.html' in file and not os.path.exists(os.path.join(path, 'html_files', file)):
shutil.move(os.path.join(path, file), os.path.join(path, 'html_files', file))
# Move PDF files to the 'pdf_files' folder
elif '.pdf' in file and not os.path.exists(os.path.join(path, 'pdf_files', file)):
shutil.move(os.path.join(path, file), os.path.join(path, 'pdf_files', file))
# Move TXT files to the 'text_files' folder
elif '.txt' in file and not os.path.exists(os.path.join(path, 'text_files', file)):
shutil.move(os.path.join(path, file), os.path.join(path, 'text_files', file))
Error Handling
You might have noticed a little change in the code structure where we had to move the files with the .lnk
extension. if you look again, you will notice that I nested a try-and-accept
block in the elif
statement for the .lnk
files.
This was because I encountered a challenge moving the files with .lnk
extensions. I added it to notify me if the file was moved successfully, if the file already exists in the folder or if there was an error. Thankfully after doing this, the issue was resolved. your code might run smoothly without this addition but in case you run into a similar issue, this try and except
block might be the solution.
#error handling
elif '.lnk' in file:
source_file_path = os.path.join(path, file)
destination_file_path = os.path.join(path, 'links', file)
if not os.path.exists(destination_file_path):
try:
shutil.move(source_file_path, destination_file_path)
print(f"Moved '{file}' to the 'links' folder.")
except Exception as e:
print(f"Error moving '{file}': {e}")
else:
print(f"'{file}' already exists in the 'links' folder.")
Let's Try It Out!
Congratulations on getting this far! Now it's time to see if our software works as we intended it to. Check the source directory you used in your code (that in this tutorial was the desktop). Are all files sorted according to their file types? (or the specific criteria you used?) if not, you likely have one or more bugs in your code that you need to fix. Debugging might not be the most fun topic for any beginner but the process of reviewing your code and trying to fix issues is a huge learning opportunity that concretises your learning and exposes you to more concepts in the process.
A Little Extra
I just thought to share some ideas on how you can improve this software, get an improved project and learn so much more in the process, are you up for the challenge?
- Schedule Automation: While users can use their computer's native scheduling software to ensure that files get organised at periodic intervals, you can also explore using scheduling libraries to set up automation.
User-friendly interface: creating a user-friendly interface will make this software accessible to more people especially those who are coding-averse. making it easier for them to customise their sorting rules.
And It's a Wrap!
Congratulations on completing this tutorial and hopefuly one beginner project you are proud of. I encourage you always practice while you learn and explore ways to use you knowledge to solve real life problems, be patient with yourself and know that your skills and competence will increase if you persist on this journey.