DEV Community

Nincy
Nincy

Posted on • Edited on

My Desktop Was a Mess β€” So I Wrote a Python Script!!

Hey Devs πŸ‘‹

I'm currently learning Python and ones of the mini-projects I rencently built was a real-life scrip to clean up my messy desktop. Here's what I wanted to do, specifically:

  • Move all .jpg and .png screenshots from one folder to another.

  • Rename them in a structured way (like 0_filenameΒ΄.png, 1_filename.jpg, and so forth)

A really great little exercise to practice file manipulation and loops - and to be honest, that is stressful but nice at the same time.

🧠 What the Scipt Does
Here's the full code:

import pathlib

# Find the path to my Desktop
home = pathlib.Path(r"C:\Users\lopez\OneDrive\Escritorio")
screenshots = home / "screenshots"

# Create screenshots folder if it doesn't exist
if not screenshots.exists():
    screenshots.mkdir()

# Move screenshots
for filepath in home.iterdir():
    if filepath.is_file() and filepath.suffix.lower() in [".jpg", ".png"]:
        destination = screenshots / filepath.name
        print("Moving:", filepath, "to", destination)
        filepath.replace(destination)

Enter fullscreen mode Exit fullscreen mode

("Escritorio" because my language is Spanish, but if yours is in English would be "Desktop")

So now, πŸ” LET'S BREAK IT DOWN πŸ”

  1. Import pathlib: This imports Python’s pathlib module, for when you want to do file paths in a much nicer, object-oriented way. It’s a lot more human-readable than fiddling with os.path.

  2. screenshots: We set the path to the directory containing my original screenshots. Now, that will surely tell your Python: β€œCheck out this folder!”

  3. destination.mkdir(exist_ok=True): Finally, we make a new folder where the images with their new names will place. The exist_ok=True tells Python not to complain if the folder does not exist.

  4. for index, filepath in enumerate(...): Enumerating all files in the screenshots folder so that we have an index (0, 1, 2...) to rename files sequentially.

  5. if filepath.suffix == ".jpg" or filepath.suffix == ".png" : Checks if the file is of type .jpg or .png

  6. screenshot = destination.joinpath(...) : Constructs a new path within the destination folder with the file renamed, that is to say, with no white space composite ’’, etc. is changed to β€˜_’

  7. filepath.replace(screenshot) : The last part moves the file from its original folder to the newly created one with a new name.

πŸ§ͺ Output Example πŸ§ͺ
This is the output when running:

  • Print result: C:\Users\lopez\OneDrive\Escritorio\renamed_screenshots\0_image1.png
  • Print result: C:\Users\lopez\OneDrive\Escritorio\renamed_screenshots\1_screenshot2.jpg ...

And just like that, my screenshots are all neatly organized and renamed!

πŸ€” What I Learned πŸ€”

  • How to use pathlib for modern path handling.
  • How to create directories in Python.
  • How to loop through files and rename them on the fly.
  • Most importantly, how rewarding it is to write scripts that tackle real-life challenges!

πŸš€ Final Thoughts πŸš€
If you're diving into Python, I can't recommend enough that you try building small tools like this. It gives you instant feedback and makes your learning experience feel incredibly practical.

Top comments (1)

Collapse
 
nathan_tarbert profile image
Nathan Tarbert

Pretty cool how you just handled it yourself and made life easier. Makes me want to write a script for my own clutter.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.