Move Downloaded Pictures from the "Downloads" Directory into another Directory by using a Python Script

Move Downloaded Pictures from the "Downloads" Directory into another Directory by using a Python Script

Overview

This article explains how to write a Python script that is intended to run in the "Downloads" directory to move all downloaded images that should appear in the "Downloads" folder to the Pictures directory or another specified directory.

This tutorial shows step-by-step how the Python script in Windows is constructed. At the end of the article, there are separate scripts that can be run in Linux or MacOS.

Materials

  • Computer

  • Powershell, Command Prompt, Terminal or IDE

  • Python

Procedure

Begin writing the Python script and import modules os, glob, and shutil:

#!/usr/bin/env python3

import os
import glob
import shutil

To use the modules all on one line, it can be written with many commas to separate and read them:

import os, glob, shutil

The os module is used to directly access the directories, while glob is used to search through specified directories for files with the ".png" type and shutil moves the ".png" files to another directory.

This script is intended to run in the "Downloads" directory. A few variables were declared in advance to see if the script is being run in the "Downloads" directory and these variables are needed to execute an if and else statement and a for loop.

download_path = os.getcwd()

png_images = glob.glob("*.png")
picture_png_entry = 0

"download_path" value is assigned to "os.getcwd()". "os.getcwd()" gives the current path or directory where the script is currently being run from. If this script is run in the "Downloads" directory, it will give us the directory of the "Downloads" path.

"png_images" is equal to "glob.glob("*.png")" as glob.glob() searches for any specified file. The specified files that are being searched will be any file that has the file type ".png". Having the * ensures, any file name with the ".png" file type will be searched for.

An if_and_else statement can be declared to check if the script is running in the downloads folder. It will then execute a for_loop by going through the "png_images" which is assigned to "glob.glob()" to search through all the ".png" images. The "picture_png_entry = 0" was declared and increased by one to search through all the elements with a ".png" file type and keep track of each ".png" unique name. When moving a picture to a directory, all the directory and file names must be case-sensitive in a format such as this depending on which operating system is being used:

directory\directory\directory\filename.extension (For Windows) or directory/directory/directory/filename.extension (For Linux or Mac)

Looping through with "glob.glob()", gives all the unique images with their file extension back to us and every image name for a temporary time. A new variable can be declared to combine the directory path information followed by the file name retrieved from "glob.glob()" to show us the original_path of the downloaded image. "shutil.move()" requires two arguments, the original_path of the file, and the new path that needs to be specified to move the image to another directory.

if download_path == r"insert_file_download_path":
    for i in png_images:
        current_image = png_images[picture_png_entry]
        str_current_image = str(current_image)

        original_path = download_path + "\\" + str_current_image        
        print(original_path)

        shutil.move( os.path.abspath(str_current_image), os.path.abspath(r"directory_path_where_pictures_will_be_moved_to") + "\\" + str_current_image )

        picture_png_entry += 1

    print("Success. Images moved!")
else:
    print("Not in the same directory. No images moved.")

As stated before, the "shutil.move()" takes two arguments since arguments are separated by commas. The second argument is taking a long string sentence for the new directory where the pictures will be moved. The "os.path.abspath()" command is used for both "shutil.move()" arguments to ensure the full directory path is read and used.

Windows ".png" Script

#!/usr/bin/env python3
# Script to Organize .png files

import os, glob, shutil

download_path = os.getcwd()

png_images = glob.glob("*.png")
picture_png_entry = 0

if download_path == r"insert_file_download_path":
    for i in png_images:
        current_image = png_images[picture_png_entry]
        str_current_image = str(current_image)

        original_path = download_path + "\\" + str_current_image        
        print(original_path)

        shutil.move( os.path.abspath(str_current_image), os.path.abspath(r"directory_path_where_pictures_will_be_moved_to") + "\\" + str_current_image )

        picture_png_entry += 1

    print("Success. Images moved!")
else:
    print("Not in the same directory. No images moved.")

Windows ".JPG" Script

#!/usr/bin/env python3
# Script to Organize .JPG files

import os, glob, shutil

download_path = os.getcwd()

jpg_images = glob.glob("*.JPG")
picture_JPG_entry = 0

if download_path == r"insert_file_download_path":
    for i in jpg_images:
        current_image = jpg_images[picture_JPG_entry]
        str_current_image = str(current_image)

        original_path = download_path + "\\" + str_current_image
        print(original_path)

        shutil.move( os.path.abspath(str_current_image), os.path.abspath(r"directory_path_where_pictures_will_be_moved_to") + "\\" + str_current_image )

        picture_JPG_entry += 1

    print("Success. Images moved!")
else:
    print("Not in the same directory. No images moved.")

Linux or MacOS ".png" Script

#!/usr/bin/env python3
# Script to Organize .png files

import os, glob, shutil

download_path = os.getcwd()

png_images = glob.glob("*.png")
picture_png_entry = 0

if download_path == r"insert_file_download_path":
    for i in png_images:
        current_image = png_images[picture_png_entry]
        str_current_image = str(current_image)

        original_path = download_path + "/" + str_current_image        
        print(original_path)

        shutil.move( os.path.abspath(str_current_image), os.path.abspath(r"directory_path_where_pictures_will_be_moved_to") + "/" + str_current_image )

        picture_png_entry += 1

    print("Success. Images moved!")
else:
    print("Not in the same directory. No images moved.")

Linux or MacOS ".JPG" Script

#!/usr/bin/env python3
# Script to Organize .JPG files

import os, glob, shutil

download_path = os.getcwd()

jpg_images = glob.glob("*.jpg")
picture_JPG_entry = 0

if download_path == r"insert_file_download_path":
    for i in jpg_images:
        current_image = jpg_images[picture_JPG_entry]
        str_current_image = str(current_image)

        original_path = download_path + "/" + str_current_image
        print(original_path)

        shutil.move( os.path.abspath(str_current_image), os.path.abspath(r"directory_path_where_pictures_will_be_moved_to") + "/" + str_current_image )

        picture_JPG_entry += 1

    print("Success. Images moved!")
else:
    print("Not in the same directory. No images moved.")

Source Code for Windows

https://github.com/AndrewDass1/SCRIPTS/tree/main/Windows/Python/Moving%20Pictures

Source Code for Linux or MacOS

https://github.com/AndrewDass1/SCRIPTS/tree/main/Linux/Python/Moving%20Pictures