Home  >  Article  >  Backend Development  >  Fun File Mover Script: From Desktop to Screenshots Folder

Fun File Mover Script: From Desktop to Screenshots Folder

王林
王林Original
2024-09-10 06:39:39913browse

Fun File Mover Script: From Desktop to Screenshots Folder

Ever felt overwhelmed by the clutter on your desktop? ?️ If you're like me, your desktop might be a treasure trove of random files. One common culprit? Screenshots! ? They pile up faster than you can say "file management."

So, I decided to tackle this problem with a fun little script. Here’s how you can move all those screenshots (or any specific file type) from your desktop to a designated folder, all while keeping things neat and organized. ?

What You’ll Need
Python installed on your computer.
pathlib library (comes with Python, so no extra installations required!).

The Script
Here's the script that does all the heavy lifting:

import pathlib

Set the paths
desktop = pathlib.Path('/Users/91763/Desktop')
new_path = pathlib.Path('/Users/91763/Desktop/Screenshots')

Create the destination folder if it doesn't exist
new_path.mkdir(exist_ok=True)

Move all PNG files from the desktop to the new folder
for filepath in desktop.iterdir():
if filepath.suffix == '.png':
new_filepath = new_path.joinpath(filepath.name)
filepath.replace(new_filepath)

Breaking It Down

Setting Paths: We start by defining the paths for our desktop and the new folder where we want to move our screenshots.

Creating the Folder: new_path.mkdir(exist_ok=True) ensures that our new folder exists. If it doesn’t, it creates one for us.

Finding and Moving Files: We loop through each file on the desktop using desktop.iterdir(). If the file is a PNG (you can change this to any extension you like!), it gets moved to the new folder.

Why It’s Cool

Automation: No more manually sorting through files.

Organization: Keeps your desktop clean and your screenshots neatly tucked away.

Simplicity: The script is straightforward and easy to adapt for other file types or destinations.

Try this out and watch your desktop transform from a chaotic mess into a model of organization! ? If you have any questions or suggestions, drop them below. Happy coding! ?

The above is the detailed content of Fun File Mover Script: From Desktop to Screenshots Folder. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn