Home > Backend Development > Python Tutorial > Moving .png files from one folder to another using Python

Moving .png files from one folder to another using Python

Susan Sarandon
Release: 2024-10-24 06:21:30
Original
576 people have browsed it

Before attempting; make sure to have python installed on your computer.

In the python IDE you will need to start with importing the pathlib and os libraries. Both are part of the python standard library so no external installations are necessary.

1.)Import the necessary libraries (pathlib and os).
2.)Find the path to your Desktop.
3.)Create a new folder called "Screenshots" (if it doesn’t already exist).
4.)Filter files on the Desktop to find only .png files (which are usually screenshots).
5.)Move each .png file to the "Screenshots" folder

To clarify we can dive into each step a little deeper.

1.)To import pathlib and os, open your IDE, make sure your language is set to the current version of python and type:

from pathlib import Path
import os

2.) Find the Path to the Desktop
In order to move files from your Desktop, we first need to find its path. The Path.home() method returns the home directory of the current user, and we can append "Desktop" to it.

desktop = Path.home().joinpath("Desktop")

This line of code creates a Path object that points to the user's Desktop. You can confirm this by printing the path:

print(desktop)

3.) Create a new folder for Screenshots

Next, we create a new folder where we will move the .png files. The mkdir method will create the "Screenshots" folder inside the Desktop if it doesn’t already exist. The exist_ok=True argument ensures that the script won't throw an error if the folder already exists.

desktop.joinpath("Screenshots").mkdir(exist_ok=True)

  1. Filter for PNG Files

We use a simple loop to iterate over all the files on the Desktop. The iterdir() method returns an iterator for all the items in the directory.

To filter for .png files, we check two conditions:

1.) The item must be a file (f.is_file()).
2.) The file extension must be .png (f.suffix == ".png").

for f in desktop.iterdir():
if f.is_file() and f.suffix == ".png":

5.) Move the Screenshots

Finally, for each .png file, we move it to the "Screenshots" folder. This is done using the replace method, which allows us to move the file from its current location to the new path.

f.replace(desktop.joinpath("Screenshots").joinpath(f.name))

Your final input code should look like this:

Moving .png files from one folder to another using Python

Conclusion:

This script is a useful tool for organizing your Desktop by moving all .png files (typically screenshots) into a designated folder. With a few modifications, this script could be adapted to handle other file types or directories.

The above is the detailed content of Moving .png files from one folder to another using Python. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template