Copy image to another folder python

I want to copy all my JPG files in one directory to a new directory. How can I solve this in Python?I just start to learn Python.

Thanks for your reply.

asked Aug 10, 2012 at 13:47

1

Of course Python offers all the tools you need. To copy files, you can use shutil.copy(). To find all JPEG files in the source directory, you can use glob.iglob().

import glob
import shutil
import os

src_dir = "your/source/dir"
dst_dir = "your/destination/dir"
for jpgfile in glob.iglob(os.path.join(src_dir, "*.jpg")):
    shutil.copy(jpgfile, dst_dir)

Note that this will overwrite all files with matching names in the destination directory.

answered Aug 10, 2012 at 13:50

2

import shutil 
import os 

for file in os.listdir(path):
    if file.endswith(".jpg"):
       src_dir = "your/source/dir"
       dst_dir = "your/dest/dir"
       shutil.move(src_dir,dst_dir)

answered Mar 27, 2014 at 2:33

1

Just use the following code

import shutil, os
files = ['file1.txt', 'file2.txt', 'file3.txt']
for f in files:
    shutil.copy(f, 'dest_folder')

N.B.: You're in the current directory. If You have a different directory, then add the path in the files list. i.e:

files = ['/home/bucket/file1.txt', '/etc/bucket/file2.txt', '/var/bucket/file3.txt']

answered Aug 22, 2019 at 11:36

Copy image to another folder python

skpaikskpaik

3203 silver badges10 bronze badges

2

for jpgfile in glob.iglob(os.path.join(src_dir, "*", "*.jpg")):
    shutil.copy(jpgfile, dst_dir) 

You should write "**" before ".jpg" to search child directories. more "" means more subdirectory to search

Copy image to another folder python

Sociopath

12.6k18 gold badges43 silver badges71 bronze badges

answered Jan 5, 2013 at 10:46

In this short guide, you’ll see how to copy a file, from one folder to another, using Python.

To start, here is a template that you may use to copy a file in Python using shutil.copyfile:

import shutil

original = r'original path where the file is currently stored\file name.file extension'
target = r'target path where the file will be copied\file name.file extension'

shutil.copyfile(original, target)

Let’s now see the steps to apply the above template in practice.

Step 1: Capture the original path

To begin, capture the path where your file is currently stored.

For example, let’s suppose that a CSV file is stored in a folder called Test_1:

C:\Users\Ron\Desktop\Test_1\products.csv

Where the CSV file name is ‘products‘ and the file extension is csv.

Step 2: Capture the target path

Next, capture the target path where you’d like to copy the file.

For our example, the file will be copied into a folder called Test_2:

C:\Users\Ron\Desktop\Test_2\products.csv

Step 3: Copy the file in Python using shutil.copyfile

For the final step, use the following template to copy your file:

import shutil

original = r'original path where the file is currently stored\file name.file extension'
target = r'target path where the file will be copied\file name.file extension'

shutil.copyfile(original, target)

Make sure to place the ‘r‘ character before your paths to avoid the following error:

SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape

In the context of our example, the complete code would look like this:

import shutil

original = r'C:\Users\Ron\Desktop\Test_1\products.csv'
target = r'C:\Users\Ron\Desktop\Test_2\products.csv'

shutil.copyfile(original, target)

If you run the code in Python (adjusted to your paths), you’ll see that the ‘products‘ CSV file would be copied into the Test_2 folder.

Alternatively, you may copy a file with a new name.

For instance, let’s copy the original CSV file (with the file name of ‘products‘) to the new location with a new file name (‘new_products‘):

import shutil

original = r'C:\Users\Ron\Desktop\Test_1\products.csv'
target = r'C:\Users\Ron\Desktop\Test_2\new_products.csv'

shutil.copyfile(original, target)

The new file name (called ‘new_products‘) would then be copied in the target location (the Test_2 folder).

The same principles would apply for other file types. For instance, let’s suppose that a JPG file called ‘image‘ is stored in the Test_1 folder.

The following code can then be used to copy the image to the Test_2 folder:

import shutil

original = r'C:\Users\Ron\Desktop\Test_1\image.jpg'
target = r'C:\Users\Ron\Desktop\Test_2\image.jpg'

shutil.copyfile(original, target)

The JPG file should now appear in the Test_2 folder.

How do you copy a file from a folder to another folder in Python?

The below steps show how to copy a file from one folder to another..
Find the path of a file. We can copy a file using both relative path and absolute path. ... .
Use the shutil.copy() function. ... .
Use the os.listdir() and shutil copy() function to copy all files. ... .
Use copytree() function to copy entire directory..

How do I copy a file from one path to another in Python?

copytree() method. This function moves a file and any subdirectories it contains from one directory to another. This indicates that both the source and the destination include the file. The string must contain the names of both parameters.

How do I put images in a folder in Python?

“python save images to folder” Code Answer's.
from PIL import Image..
import os..
directory = r'D:\PATH'.
for filename in os. listdir(directory):.
if filename. endswith(".jpg"):.
im = Image. open(filename).

How do I select an image from a directory in Python?

getting image from path python.
from PIL import Image..
import glob..
image_list = [].
for filename in glob. glob('yourpath/*.gif'): #assuming gif..
im=Image. open(filename).
image_list. append(im).