<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: DavidIraheta</title>
    <description>The latest articles on DEV Community by DavidIraheta (@davidiraheta).</description>
    <link>https://dev.to/davidiraheta</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2259640%2F06b993ce-fb1a-4a1d-80c4-3ce795455c19.png</url>
      <title>DEV Community: DavidIraheta</title>
      <link>https://dev.to/davidiraheta</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/davidiraheta"/>
    <language>en</language>
    <item>
      <title>Moving .png files from one folder to another using Python</title>
      <dc:creator>DavidIraheta</dc:creator>
      <pubDate>Wed, 23 Oct 2024 16:45:06 +0000</pubDate>
      <link>https://dev.to/davidiraheta/moving-png-files-from-one-folder-to-another-using-python-2fme</link>
      <guid>https://dev.to/davidiraheta/moving-png-files-from-one-folder-to-another-using-python-2fme</guid>
      <description>&lt;p&gt;Before attempting; make sure to have python installed on your computer. &lt;/p&gt;

&lt;p&gt;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. &lt;/p&gt;

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

&lt;p&gt;To clarify we can dive into each step a little deeper. &lt;/p&gt;

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

&lt;p&gt;from pathlib import Path&lt;br&gt;
import os&lt;/p&gt;

&lt;p&gt;2.) Find the Path to the Desktop&lt;br&gt;
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.&lt;/p&gt;

&lt;p&gt;desktop = Path.home().joinpath("Desktop")&lt;/p&gt;

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

&lt;p&gt;print(desktop)&lt;/p&gt;

&lt;p&gt;3.) Create a new folder for Screenshots&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;desktop.joinpath("Screenshots").mkdir(exist_ok=True)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Filter for PNG Files&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;To filter for .png files, we check two conditions:&lt;/p&gt;

&lt;p&gt;1.) The item must be a file (f.is_file()).&lt;br&gt;
2.) The file extension must be .png (f.suffix == ".png").&lt;/p&gt;

&lt;p&gt;for f in desktop.iterdir():&lt;br&gt;
    if f.is_file() and f.suffix == ".png":&lt;/p&gt;

&lt;p&gt;5.) Move the Screenshots&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;f.replace(desktop.joinpath("Screenshots").joinpath(f.name))&lt;/p&gt;

&lt;p&gt;Your final input code should look like this: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff2dn2p9vxx92c6eev253.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff2dn2p9vxx92c6eev253.png" alt="Image description" width="800" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Conclusion:&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>python</category>
      <category>learning</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
