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)
("Escritorio" because my language is Spanish, but if yours is in English would be "Desktop")
So now, π LET'S BREAK IT DOWN π
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.
screenshots: We set the path to the directory containing my original screenshots. Now, that will surely tell your Python: βCheck out this folder!β
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.
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.
if filepath.suffix == ".jpg" or filepath.suffix == ".png" : Checks if the file is of type .jpg or .png
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 β_β
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)
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.