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.
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)
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)