Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy all JPG file in a directory to another directory in Python?

Tags:

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.

like image 537
fakelbst Avatar asked Aug 10 '12 13:08

fakelbst


1 Answers

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.

like image 73
Jolly Jumper Avatar answered Sep 22 '22 10:09

Jolly Jumper