Copy multiple files in Python

225,860

Solution 1

You can use os.listdir() to get the files in the source directory, os.path.isfile() to see if they are regular files (including symbolic links on *nix systems), and shutil.copy to do the copying.

The following code copies only the regular files from the source directory into the destination directory (I'm assuming you don't want any sub-directories copied).

import os
import shutil
src_files = os.listdir(src)
for file_name in src_files:
    full_file_name = os.path.join(src, file_name)
    if os.path.isfile(full_file_name):
        shutil.copy(full_file_name, dest)

Solution 2

If you don't want to copy the whole tree (with subdirs etc), use or glob.glob("path/to/dir/*.*") to get a list of all the filenames, loop over the list and use shutil.copy to copy each file.

for filename in glob.glob(os.path.join(source_dir, '*.*')):
    shutil.copy(filename, dest_dir)

Solution 3

Look at shutil in the Python docs, specifically the copytree command.

If the destination directory already exists, try:

shutil.copytree(source, destination, dirs_exist_ok=True)

Solution 4

import os
import shutil
os.chdir('C:\\') #Make sure you add your source and destination path below

dir_src = ("C:\\foooo\\")
dir_dst = ("C:\\toooo\\")

for filename in os.listdir(dir_src):
    if filename.endswith('.txt'):
        shutil.copy( dir_src + filename, dir_dst)
    print(filename)

Solution 5

def recursive_copy_files(source_path, destination_path, override=False):
    """
    Recursive copies files from source  to destination directory.
    :param source_path: source directory
    :param destination_path: destination directory
    :param override if True all files will be overridden otherwise skip if file exist
    :return: count of copied files
    """
    files_count = 0
    if not os.path.exists(destination_path):
        os.mkdir(destination_path)
    items = glob.glob(source_path + '/*')
    for item in items:
        if os.path.isdir(item):
            path = os.path.join(destination_path, item.split('/')[-1])
            files_count += recursive_copy_files(source_path=item, destination_path=path, override=override)
        else:
            file = os.path.join(destination_path, item.split('/')[-1])
            if not os.path.exists(file) or override:
                shutil.copyfile(item, file)
                files_count += 1
    return files_count
Share:
225,860

Related videos on Youtube

hidayat
Author by

hidayat

I am a programmer who appreciate simple well-written code and beautiful algorithms . My goal is to write maintainable and scalable software.

Updated on July 08, 2022

Comments

  • hidayat
    hidayat almost 2 years

    How to copy all the files present in one directory to another directory using Python. I have the source path and the destination path as string.

  • Steven
    Steven almost 14 years
    Note: You might have to check the glob results with os.path.isfile() to be sure they are filenames. See also GreenMatt's answer. While glob does return only the filename like os.listdir, it still returns directory names as well. The '.' pattern might be enough, as long as you don't have extensionless filenames, or dots in directory names.
  • Sven
    Sven over 8 years
    Good remark, but it may be not an option if the directory already exists for some reason as in my case.
  • calico_
    calico_ over 6 years
    It could help to give a verbal explanation of your code
  • Mohammad ElNesr
    Mohammad ElNesr over 6 years
    I think you mean overwrite, not override
  • Steve Byrne
    Steve Byrne about 6 years
    Should dest be something like C:\myfolder or C:\myfolder\filename.ext ?
  • Admin
    Admin about 6 years
    @StevenByrne Can be either, depending on if you want to also rename the file. If not, then dest is the directory name. shutil.copy(src, dst) "copies the file src to the file or directory dst.... If dst specifies a directory, the file will be copied into dst using the base filename from src."
  • citynorman
    citynorman over 5 years
    This doesn't copy subdirs
  • Ari
    Ari over 4 years
    Konstantin great answer!! helped me a lot. One suggestion though: to use os.sep instead of '/' (so it works on non-linux OS)
  • Emadpres
    Emadpres over 3 years
    @Sven For that try dirs_exist_ok=True option.
  • Timo
    Timo over 3 years
    Small improvement: leave the src_files and do for fn in os.listdir(src)
  • Justin
    Justin over 3 years
    Exactly what I needed, something to recursively copy subdirectories, thank you!
  • BCJuan
    BCJuan over 3 years
    From Python 3.8 on
  • Peter
    Peter almost 2 years
    os.mkdir(new_dest) needs to be recursive: os.makedirs(new_dest)