用 Python 复制多个文件

如何使用 Python 将一个目录中的所有文件复制到另一个目录中。 源路径和目标路径是字符串形式的。

239081 次浏览

Look at 关闭 Python 文档, specifically the 拷贝树 command.

如果目标目录已经存在,请尝试:

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

如果您不想复制整个树(使用子目录等) ,可以使用或 glob.glob("path/to/dir/*.*")来获取所有文件名的列表,循环遍历该列表并使用 shutil.copy来复制每个文件。

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

您可以使用 Os.listdir ()获取源目录中的文件,使用 Isfile ()查看它们是否是常规文件(包括 * nix 系统上的符号链接) ,使用 Shutil 收到进行复制。

下面的代码只将普通文件从源目录复制到目标目录(我假设您不希望复制任何子目录)。

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

下面是另一个递归复制函数的示例,它允许您一次复制一个文件的目录(包括子目录)的内容,我用它来解决这个问题。

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)

编辑: 如果可以的话,一定要使用 shutil.copytree(src, dest)。但这要求目标文件夹不存在。如果您需要将文件复制到现有的文件夹中,以上方法可以很好地工作!

排名最高的答案将抛出一个运行时错误,因为它试图在第5行用一个文件名加入一个列表,而这个列表应该是用一个字符串加入另一个字符串。创建另一个名为 pathSrc 的变量,并在 join 参数中使用它。我还将创建另一个名为 pathDest 的变量,并在最后一行将其与 file _ name 连接起来。我还从 shutil 导入了所需的方法,而不是整个模块。

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)