Logo

dev-resources.site

for different kinds of informations.

📷 🐍 A Python Script for organizing Desktop screenshots

Published at
11/29/2023
Categories
python
desktop
tutorial
programming
Author
Audrey Kadjar
📷 🐍 A Python Script for organizing Desktop screenshots

Do you also take a lot of screenshots and end up with a cluttered Desktop? I've experienced that frustration many times so I ended up writing a Python script to handle the tidying up for me 👩🏻‍💻

This script automatically detects all the screenshots and pictures on the Desktop and stores them in a ✨ dedicated folder ✨

Step 1: Import modules

Let's start by importing the necessary modules: os for handling operating system interactions and shutil for file-related tasks. We'll define the organize_screenshots function to contain our script's logic.

import os
import shutil

def organize_screenshots(desktop_path, screenshots_folder):

Step 2: Listing Desktop files

We begin by listing all files on the desktop using the listdir method, which returns a list of file names for a given path.

def organize_screenshots(desktop_path, screenshots_folder):
    desktop_files = os.listdir(desktop_path)

Step 3: Create Screenshots Folder

Next, we create the Screenshots folder. We use os.path.join to join path components and os.makedirs to create the directory. A check ensures that the Screenshots folder is only created once.

screenshots_path = os.path.join(desktop_path, screenshots_folder)
    if not os.path.exists(screenshots_path):
        os.makedirs(screenshots_path)

Step 4: Sorting the files

Now, comes the fun part! Let's loop through the desktop files. If a file is a screenshot or picture, we move it to the Screenshots folder using the shutil.move method with the original and destination file paths.

    for file_name in desktop_files:
        if file_name.lower().endswith(('.png', '.jpg', '.jpeg') or file_name.lower().startswith('Screen Shot')):
            file_path = os.path.join(desktop_path, file_name)
            dest_path = os.path.join(screenshots_path, file_name)

            shutil.move(file_path, dest_path)
            print(f"Moved {file_name}")

Step 5: Bringing It All Together

Finally, let's bring everything together. We call our function with the correct paths and use the if __name__ == "__main__" construct to specify that this script is intended to be run as the main program.

import os
import shutil

def organize_screenshots(desktop_path, screenshots_folder):
    desktop_files = os.listdir(desktop_path)

    screenshots_path = os.path.join(desktop_path, screenshots_folder)
    if not os.path.exists(screenshots_path):
        os.makedirs(screenshots_path)

    for file_name in desktop_files:
        if file_name.lower().endswith(('.png', '.jpg', '.jpeg') or file_name.lower().startswith('Screen Shot')):
            file_path = os.path.join(desktop_path, file_name)
            dest_path = os.path.join(screenshots_path, file_name)

            shutil.move(file_path, dest_path)
            print(f"Moved {file_name}")

if __name__ == "__main__":
    desktop_path = "../Desktop" 
    screenshots_folder = "Screenshots"

organize_screenshots(desktop_path, screenshots_folder)

To execute the script, open a CLI terminal and run the command (ensure that Python is installed on your operating system):

python3 <your_script_file_path>

Congratulations! Your cluttered Desktop is now organized! 👩🏻‍💻✨ You can find the full script in my Github repo.

Feel free to reach out if you have any questions! You can also find me on Github, LinkedIn, and Instagram.

Featured ones: