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)
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)
import os
import shutil
def recursive_copy(src, dest):
"""
Copy each file from src dir to dest dir, including sub-directories.
"""
for item in os.listdir(src):
file_path = os.path.join(src, item)
# if item is a file, copy it
if os.path.isfile(file_path):
shutil.copy(file_path, dest)
# else if item is a folder, recurse
elif os.path.isdir(file_path):
new_dest = os.path.join(dest, item)
os.mkdir(new_dest)
recursive_copy(file_path, new_dest)
import os
from shutil import copyfile
pathSrc = "the folder where the src files are"
pathDest = "the folder where the dest files are going"
src_files = os.listdir(src)
for file_name in src_files:
full_file_name = os.path.join(pathSrc, file_name)
if os.path.isfile(full_file_name):
shutil.copy(full_file_name, pathDest + file_name)
import os
import shutil
dest = 'path/to/destination/folder'
src = 'path/to/source/folder/' # mind the '/' at the end
[shutil.copy(src+fn, dest) for fn in os.listdir(src)]
或具有错误处理条件
import os
import shutil
dest = 'path/to/destination/folder'
src = 'path/to/source/folder/' # mind the '/' at the end
try:
[shutil.copy(src+fn, dest) for fn in os.listdir(src)]
except:
print('files already exist in', dest)