Copy Single or Multiple Files in Seconds Using Python

In this tutorial, we are going to learn how to copy single or multiple files in a second using the Python shutil library. File extension is any like ‘.jpg’, ‘png’, ‘.txt’.

Syntax: shutil.copy(source_file_path, destination_file_path)

IInstall Python Shutil Library

Enter below command in python environment

pip install pytest-shutil

Import Libraries

import shutil # copy the file
import glob # get the path of files

Copy Single File

img_path = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\pexels-harsh-raj-gond-1485031.jpg"
dest_img_path = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\copy\pexels-harsh-raj-gond-1485031.jpg"

shutil.copy(img_path, dest_img_path) # copyt the file

Copy Multiple Files at once

dir_path = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data"

img_paths = glob.glob(dir_path + "\\*.jpg")

dest_img_dir_path = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\copy"

for img_path in img_paths:
    img_name = img_path.split("""\\""")[-1]
    img_dest_full_path = dest_img_dir_path + """\\""" + img_name
    shutil.copy(img_path, img_dest_full_path) ## copyt the file

Leave a Reply

Top