📷 🐍 A Python Script for organizing Desktop screenshots
Tags: python, desktop, tutorial, programming
Published: 2023-11-29 07:28:15

Do you also take a lot of screenshots and end up with a cluttered Desktop? I've experienced that frustration many times, inspiring me to find a solution. 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.

```python
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.

```python
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.

```python
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.

```python
    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.

```python
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](https://www.python.org/downloads/)):

```bash
python3 <your_script_file_path>
```

Congratulations! Your cluttered Desktop is now organized! 👩🏻‍💻✨

Feel free to reach out if you have any questions! You can also find me on [Github](https://github.com/AudreyKj), [LinkedIn](https://www.linkedin.com/in/audreykadjar/), and [Instagram](https://www.instagram.com/audreykadjar/).