Created
April 15, 2024 15:32
-
-
Save linuxinsights/199d4c7575d0a9c7e8844193f3f1fc21 to your computer and use it in GitHub Desktop.
Organize Desktop Files with Python | https://linuxtech.in/organize-desktop-files-with-python/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment