My Desktop Cleaner 🧹 — A Python Script for Efficient File Management
If you’ve ever done automation programs, you’re likely aware of the advantages they offer beyond simplifying enterprise activities.
Python, with its versatility, becomes an ideal choice for such tasks.
Recently, my admiration for Python has grown, not necessarily for its potentials in corporate settings, but for its ability to enhance personal productivity by automating mundane tasks.
Have you ever felt annoyed by the clutter on your desktop? You download or create files, and they just stay there until you manually organize them.
Ugh! I don’t know for you, but it’s so boring.
That’s why I’ve got a simple and effective Python script that moves files to their proper places on my desktop. Let’s break it down.
Importing Modules and Setting the Stage
import os
import shutil
def move_and_arrange(desk_path, inp1, inp2):
dst_path = os.path.join(desk_path, inp1)
src_dst = os.chdir(desk_path)
lst = os.listdir()
The first lines involve importing the necessary modules. Here, I import os
for handling files and directories, and shutil
for moving them.
The move_and_arrange
function is defined to handle the core operations, including setting the destination path and listing the contents of the desktop.
Going Through the Files
for file in lst:
if file.endswith(inp2):
file_path = os.path.join(desk_path, file)
if not os.path.exists(os.path.join(dst_path, file)):
print(f"Moving {file} to {dst_path}")
shutil.move(file_path, dst_path)
else:
print(f"File {file} already exists in {dst_path}")
This part loops through my desktop files, checks if they match the specified format (inp2
), and moves them to the destination if they do; providing feedback on the process.
It also avoids moving files that already exist in the destination.
User Interaction
user_input = input('Do you want to auto-arrange the desktop? (yes/no): ').lower()
if user_input == 'yes':
auto_arrange(desk_path)
else:
quit()
After moving files, the script asks if I want to auto-arrange my entire desktop.
If “yes”, it proceeds to the next step. If “no”, the program quits. Simple English.
The reason this is brought up is that In some instances, I may simply want to move specific files to a designated directory without immediately opting to auto-arrange the desktop. This scenario may arise when I’m still actively working on other files. On other occasions, I might prefer to conduct a general cleanup. Hence, the user_input is set to ‘yes’.
Automating Desktop Arrangement
def auto_arrange(desk_path):
standard_folders = ['Documents', 'Images', 'Videos']
for folder in standard_folders:
folder_path = os.path.join(desk_path, folder)
if not os.path.exists(folder_path):
os.makedirs(folder_path)
The auto_arrange
function is introduced, creating standard folders (Documents, Images, Videos) if they don't already exist on the desktop.
Moving the Files
for file in os.listdir(desk_path):
file_path = os.path.join(desk_path, file)
if os.path.isfile(file_path):
category = get_file_category(file)
if category:
category_path = os.path.join(desk_path, category)
count = 1
new_file_path = os.path.join(category_path, file)
while os.path.exists(new_file_path):
base_name, extension = os.path.splitext(file)
new_file_path = os.path.join(category_path, f"{base_name}_{count}{extension}")
count += 1
print(f"Moving {file} to {new_file_path}")
shutil.move(file_path, new_file_path)
This section of the code orchestrates the categorization and relocation of files to their respective folders (Images, Videos, Documents).
It also addresses scenarios where files with the same names exist, ensuring uniqueness through iterative naming.
The control flows including the conditions and the while loops handle this perfectly.
Categorizing the Files
def get_file_category(file_name):
image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp']
video_extensions = ['.mp4', '.avi', '.mkv', '.mov']
document_extensions = ['.txt', '.docx', '.pdf', '.xlsx']
ext = os.path.splitext(file_name)[1].lower()
if ext in image_extensions:
return 'Images'
elif ext in video_extensions:
return 'Videos'
elif ext in document_extensions:
return 'Documents'
else:
pass
The get_file_category
function categorizes files based on their extensions provided in python lists, giving a comprehensive framework for file organization.
Finally, Executing the Script
destination_folder = input('Enter the destination folder: ')
file_format = input('Enter the file format: ')
desktop_path = r"C:\users\this pc\desktop"
move_and_arrange(desktop_path, destination_folder, file_format)
This final segment is where I kick off the script.
The block initiates script execution by gathering user inputs for the destination folder and file format, setting the desktop path, and invoking the move_and_arrange
function.
With this breakdown, you can see how each part contributes to the overall functionality of the script.
It’s a simple yet powerful way to keep my desktop organized without the hassle of manual file management.
Now, I will run the script…
And viola ! neat 🧹✨
Efficiency of the Script
- Automation: The Python script efficiently automates the process of organizing files on the desktop, eliminating the need for manual intervention.
- Customizability: The script is highly customizable, allowing users to define their destination folders and file formats. This level of flexibility makes it adaptable to individual preferences and diverse file management needs. I can easily modify the script to accommodate changes in file categorization or desktop organization requirements.
Setbacks
- Limited File Categorization Logic: The script categorizes files based on a predefined set of extensions for images, videos, and documents. However, this approach may be limited as new file formats emerge or if users have unconventional file categorization needs.
- Interaction Limited to Command Line: The user interaction is primarily through the command line — by running
>>> python script_name on Windows OS or
>>> python3 script_name on Linux,
which may not be the most user-friendly interface for all users.
But enhancements could involve developing a graphical user interface (GUI) or a more intuitive user experience to make the script accessible to individuals who may not be comfortable with command-line interactions.
While the script presents notable advantages in terms of automation and customizability, addressing the identified setbacks could contribute to further refinement and user-friendliness.
Check out the full code here: https://github.com/Cyber-philsopher/Local-Automation-Scripts. You can pick it up for your own personal use as well. Or better still, learn from it.
So, that’s it. I hope you grabbed some py(s)🥧
Catch you in the next one.