copy file to another directory python code example

Example 1: copy files python

from shutil import copyfile
copyfile(src, dst)

Example 2: python copy file to another directory

import shutil
shutil.copy2('/src/dir/file.ext', '/dst/dir/newname.ext') # complete target filename given
shutil.copy2('/src/file.ext', '/dst/dir') # target filename is /dst/dir/file.ext

Example 3: copy image from one folder to another in python

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)

Example 4: copy directory from one location to another python

# Source path 
src = 'C:/Users / Rajnish / Desktop / GeeksforGeeks / source'
   
# Destination path 
dest = 'C:/Users / Rajnish / Desktop / GeeksforGeeks / destination'
   
# Copy the content of 
# source to destination 
destination = shutil.copytree(src, dest)

Example 5: copyfile pyhon

from shutil import copyfile
copyfile(src, dst)