Question
Asked By – hidayat
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.
Now we will see solution for issue: Copy multiple files in Python
Answer
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)
This question is answered By – GreenMatt
This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0