There is a tendency for files to accumulate on the desktop over time, making it difficult to locate the specific files that you want. The purpose of this Python script, “Organize Desktop Files with Python,” is to organize the files that are located on your desktop into folders according to the extensions of the files. By using this script, you will be able to have a more organized desktop and find your files with more ease.

desktop_sorter.py

import argparse
import os
import shutil
def sort_files_on_desktop(desktop_path):
# Create a dictionary mapping file extensions to folder names
folder_names = {
".txt": "Text Files",
".pdf": "PDFs",
".doc": "Word Documents",
".docx": "Word Documents",
".ppt": "PowerPoint Presentations",
".pptx": "PowerPoint Presentations",
".xls": "Excel Spreadsheets",
".xlsx": "Excel Spreadsheets",
".jpg": "Images",
".jpeg": "Images",
".png": "Images",
".gif": "Images",
".bmp": "Images",
".tiff": "Images",
".mp4": "Videos",
".mov": "Videos",
".avi": "Videos",
".mkv": "Videos",
".mp3": "Music",
".wav": "Music",
".flac": "Music",
".py": "Python Files",
".ipynb": "Jupyter Notebooks",
".java": "Java Files",
".class": "Java Compiled Files",
".jar": "Java Archives",
".cpp": "C++ Files",
".h": "Header Files",
".c": "C Files",
".obj": "Object Files",
".o": "Object Files",
".exe": "Executable Files",
".tar": "Archives",
".zip": "Archives",
".tar.gz": "Archives",
".svg": "Images",
".deb": "Package Files",
".iso": "Disk Images",
}
# Loop through each file on the desktop
for file_name in os.listdir(desktop_path):
file_path = os.path.join(desktop_path, file_name)
if os.path.isfile(file_path):
file_extension = os.path.splitext(file_name)[1].lower()
if file_extension in folder_names:
folder_name = folder_names[file_extension]
folder_path = os.path.join(desktop_path, folder_name)
if not os.path.exists(folder_path):
os.mkdir(folder_path)
new_file_path = os.path.join(folder_path, file_name)
shutil.move(file_path, new_file_path)
def main():
# Create the argument parser
parser = argparse.ArgumentParser(description='Sort files on the desktop into folders based on file extensions')
parser.add_argument('--desktop_path', type=str, help='The path to the desktop')
args = parser.parse_args()
# Get the desktop path from command line arguments
desktop_path = args.desktop_path
if desktop_path:
sort_files_on_desktop(desktop_path)
print("Files sorted successfully.")
else:
print("Please provide a valid desktop path using --desktop_path argument.")
if __name__ == "__main__":
main()

Script Usage

To use the script, you will need Python installed on your computer. Once Python is installed, open a command prompt or terminal and navigate to the directory where the script is located. Then, run the following command:


python desktop_sorter.py --desktop_path /path/to/desktop

Replace <path-to-desktop> with the actual path to your desktop. For example, on Windows, the path could be C:\Users\username\Desktop, while on Mac, it could be /Users/username/Desktop.

How The Code Works

Argument Parsing

The script uses argparse to define and parse command-line arguments.

Specifically, it expects a --desktop_path argument which specifies the path to the desktop directory.

File Sorting Process

The script defines a mapping (folder_names) from file extensions to folder names. This mapping determines where each file will be moved based on its extension.

Iterating Over Files

It iterates through all files in the specified desktop directory (desktop_path).

For each file, it checks if it’s a regular file (not a directory).

Determining Destination Folder

Extracts the file extension using os.path.splitext(file_name)[1].

Checks if the file extension exists in folder_names.

Moving Files

If the file extension corresponds to a folder name in folder_names, it creates the destination folder if it doesn’t exist.

Moves the file to the appropriate folder based on its extension using shutil.move.

Main Function

The main function orchestrates the entire process:

Parses command-line arguments to get the desktop path.

Calls sort_files_on_desktop with the specified desktop path to sort the files.

By running this script with the --desktop_path argument pointing to your desktop directory, it will organize the files on your desktop into folders based on their file extensions according to the defined mapping (folder_names). This helps in keeping the desktop organized and categorizing files by type.

See also: Other useful Python Scripts

Got any queries or feedback? Feel free to drop a comment below!

Categorized in: