如何复制文件

3747

如何在Python中复制文件?


3
如果您指定复制的原因,那就太好了。对于许多应用程序而言,硬链接可能是一个可行的替代方案。而且寻找这种解决方案的人也可以考虑这个“复制”机会(就像我一样,但我的答案在这里被投了反对票)。 - Yaroslav Nikitenko
27个回答

4611

shutil有许多您可以使用的方法,其中之一是:

import shutil

shutil.copyfile(src, dst)

# 2nd option
shutil.copy(src, dst)  # dst can be a folder; use shutil.copy2() to preserve timestamp
  • 将名为src的文件内容复制到名为dst的文件中。 srcdst 都需要是完整的文件名,包括路径。
  • 目标位置必须可写;否则,将引发IOError异常。
  • 如果dst已经存在,它将被替换。
  • 此函数无法复制字符设备、块设备和管道等特殊文件。
  • 对于copysrcdst是作为字符串给出的路径名。

另一个要看的shutil方法是shutil.copy2()。它类似但会保留更多元数据(例如时间戳)。

如果您使用os.path操作,请使用copy而不是copyfilecopyfile只接受字符串。


8
在Python 3.8中,这得到了一些显著的速度提升(取决于操作系统,速度提高了约50%)。 - Martijn Pieters
2
目标文件夹是否需要存在? - KansaiRobot
2
@KansaiRobot:是的,否则你会得到一个异常:FileNotFoundError: 目录不存在:foo/ - John
它不会复制文件元数据,比如文件所有者的组在POSIX操作系统(GNU/Linux,FreeBSD等)上。 - undefined

2094

10
请注意,即使是shutil.copy2()函数也无法复制所有文件元数据。 - wovano

967

copy2(src,dst)copyfile(src,dst)更实用,因为:

  • 它允许dst是一个目录(而不是完整的目标文件名),在这种情况下,使用src基本名称来创建新文件;
  • 它保留文件元数据中的原始修改和访问信息(mtime和atime)(但这会带来轻微的开销)。

以下是一个简短的示例:

import shutil
shutil.copy2('/src/dir/file.ext', '/dst/dir/newname.ext') # complete target filename given
shutil.copy2('/src/file.ext', '/dst/dir') # target filename is /dst/dir/file.ext

271

在Python中,您可以使用以下模块来复制文件:


import os
import shutil
import subprocess

1)使用shutil模块复制文件

shutil.copyfile 方法签名

shutil.copyfile(src_file, dest_file, *, follow_symlinks=True)

# example    
shutil.copyfile('source.txt', 'destination.txt')
shutil.copy 的签名
shutil.copy(src_file, dest_file, *, follow_symlinks=True)

# example
shutil.copy('source.txt', 'destination.txt')
shutil.copy2 签名
shutil.copy2(src_file, dest_file, *, follow_symlinks=True)

# example
shutil.copy2('source.txt', 'destination.txt')  
shutil.copyfileobj 函数的签名。
shutil.copyfileobj(src_file_object, dest_file_object[, length])

# example
file_src = 'source.txt'  
f_src = open(file_src, 'rb')

file_dest = 'destination.txt'  
f_dest = open(file_dest, 'wb')

shutil.copyfileobj(f_src, f_dest)  

2) 使用os模块复制文件

os.popen 的用法

os.popen(cmd[, mode[, bufsize]])

# example
# In Unix/Linux
os.popen('cp source.txt destination.txt') 

# In Windows
os.popen('copy source.txt destination.txt')
os.system 签名。
os.system(command)


# In Linux/Unix
os.system('cp source.txt destination.txt')  

# In Windows
os.system('copy source.txt destination.txt')

3) 使用 subprocess 模块复制文件

subprocess.call 的函数签名

subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)

# example (WARNING: setting `shell=True` might be a security-risk)
# In Linux/Unix
status = subprocess.call('cp source.txt destination.txt', shell=True) 

# In Windows
status = subprocess.call('copy source.txt destination.txt', shell=True)
subprocess.check_output 签名
subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False)

# example (WARNING: setting `shell=True` might be a security-risk)
# In Linux/Unix
status = subprocess.check_output('cp source.txt destination.txt', shell=True)

# In Windows
status = subprocess.check_output('copy source.txt destination.txt', shell=True)


3
感谢您列出的许多选项。在我看来,“os.popen('cp source.txt destination.txt')”(以及类似的代码)是糟糕的设计。Python 是一种跨平台语言,使用这样的代码会破坏这个伟大的特性。 - Yaroslav Nikitenko
6
只有第一个示例回答了这个问题。运行shell命令不是可移植的,也不能真正执行操作。 - Shriraj Hegde

186
您可以使用来自 shutil 包中的复制函数之一:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
功能                         保留权限      支持目标目录   接受文件对象  复制其他元数据
―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
shutil.copy              ✔             ✔                 ☐           ☐
shutil.copy2             ✔             ✔                 ☐           ✔
shutil.copyfile          ☐             ☐                 ☐           ☐
shutil.copyfileobj       ☐             ☐                 ✔           ☐
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

示例:

import shutil
shutil.copy('/etc/hostname', '/var/tmp/testhostname')

这与两年前的 jezrael回答 相比增加了什么? - wim
@wim,你需要将我的答案与jesrael的答案相比较,那时我已经发布了我的答案。因此,我添加了另一列,使用了更好的列标题和不那么分散注意力的表格布局。我还为最常见的情况添加了一个示例。您不是第一个问这个问题的人,我已经回答过这个问题几次了,但不幸的是,一些版主删除了旧评论。在检查我的旧笔记后,甚至出现了这样的情况:您早先就曾向我问过这个问题,而您的问题和我的答案也被删除了。 - maxschlepzig
@wim 我还在Python文档中添加了链接。新增的列包含有关目录目的地的信息,这是Jezrael的答案没有包括的。 - maxschlepzig
哦,亲爱的,我早已经问过了,这很有趣。是的,我同意文档链接和额外列是进步,但是最好将它们编辑到其他答案中。看起来一些用户随后这样做了,现在我们有两个答案,除了格式之外并没有明显区别 - 建议删除? - wim

108

复制文件是一项相对简单的操作,如下所示的示例,但你应该使用shutil标准库模块来完成。

def copyfileobj_example(source, dest, buffer_size=1024*1024):
    """      
    Copy a file from source to dest. source and dest
    must be file-like objects, i.e. any object with a read or
    write method, like for example StringIO.
    """
    while True:
        copy_buffer = source.read(buffer_size)
        if not copy_buffer:
            break
        dest.write(copy_buffer)

如果您想按文件名复制,可以像这样操作:

def copyfile_example(source, dest):
    # Beware, this example does not handle any edge cases!
    with open(source, 'rb') as src, open(dest, 'wb') as dst:
        copyfileobj_example(src, dst)

86

使用shutil模块

copyfile(src, dst)

将名为src的文件内容复制到名为dst的文件中。目标位置必须可写,否则将引发IOError异常。如果dst已经存在,则将被替换。此函数无法复制字符设备、块设备和管道等特殊文件。srcdst是以字符串形式给出的路径名。

请查看filesys,了解标准Python模块中提供的所有文件和目录处理函数。


50

一个目录和文件复制的示例,来自Tim Golden的Python Stuff

import os
import shutil
import tempfile

filename1 = tempfile.mktemp (".txt")
open (filename1, "w").close ()
filename2 = filename1 + ".copy"
print filename1, "=>", filename2

shutil.copy (filename1, filename2)

if os.path.isfile (filename2): print "Success"

dirname1 = tempfile.mktemp (".dir")
os.mkdir (dirname1)
dirname2 = dirname1 + ".copy"
print dirname1, "=>", dirname2

shutil.copytree (dirname1, dirname2)

if os.path.isdir (dirname2): print "Success"

43

对于小文件且仅使用Python内置函数,您可以使用以下一行代码:

with open(source, 'rb') as src, open(dest, 'wb') as dst: dst.write(src.read())

当文件过大或内存不足时,这不是最优的应用方式,因此最好采用Swati的答案。


1
不安全的复制方式会多次复制链接文件,因此不适用于符号链接或硬链接。 - Tcll

33

首先,我为您制作了一个详尽的 shutil 方法备忘单供您参考。

shutil_methods =
{'copy':['shutil.copyfileobj',
          'shutil.copyfile',
          'shutil.copymode',
          'shutil.copystat',
          'shutil.copy',
          'shutil.copy2',
          'shutil.copytree',],
 'move':['shutil.rmtree',
         'shutil.move',],
 'exception': ['exception shutil.SameFileError',
                 'exception shutil.Error'],
 'others':['shutil.disk_usage',
             'shutil.chown',
             'shutil.which',
             'shutil.ignore_patterns',]
}

其次,解释一些复制的方法:

  1. shutil.copyfileobj(fsrc, fdst[, length]) 操作已经打开的对象

In [3]: src = '~/Documents/Head+First+SQL.pdf'
In [4]: dst = '~/desktop'
In [5]: shutil.copyfileobj(src, dst)
AttributeError: 'str' object has no attribute 'read'

# Copy the file object
In [7]: with open(src, 'rb') as f1,open(os.path.join(dst,'test.pdf'), 'wb') as f2:
    ...:      shutil.copyfileobj(f1, f2)
In [8]: os.stat(os.path.join(dst,'test.pdf'))
Out[8]: os.stat_result(st_mode=33188, st_ino=8598319475, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516067347, st_mtime=1516067335, st_ctime=1516067345)
  • shutil.copyfile(src, dst, *, follow_symlinks=True) 复制并重命名

  • In [9]: shutil.copyfile(src, dst)
    IsADirectoryError: [Errno 21] Is a directory: ~/desktop'
    # So dst should be a filename instead of a directory name
    
  • shutil.copy() 复制文件但不保留元数据。

    In [10]: shutil.copy(src, dst)
    Out[10]: ~/desktop/Head+First+SQL.pdf'
    
    # Check their metadata
    In [25]: os.stat(src)
    Out[25]: os.stat_result(st_mode=33188, st_ino=597749, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516066425, st_mtime=1493698739, st_ctime=1514871215)
    In [26]: os.stat(os.path.join(dst, 'Head+First+SQL.pdf'))
    Out[26]: os.stat_result(st_mode=33188, st_ino=8598313736, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516066427, st_mtime=1516066425, st_ctime=1516066425)
    # st_atime,st_mtime,st_ctime changed
    
  • shutil.copy2() 保留元数据的复制

  • In [30]: shutil.copy2(src, dst)
    Out[30]: ~/desktop/Head+First+SQL.pdf'
    In [31]: os.stat(src)
    Out[31]: os.stat_result(st_mode=33188, st_ino=597749, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516067055, st_mtime=1493698739, st_ctime=1514871215)
    In [32]: os.stat(os.path.join(dst, 'Head+First+SQL.pdf'))
    Out[32]: os.stat_result(st_mode=33188, st_ino=8598313736, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516067063, st_mtime=1493698739, st_ctime=1516067055)
    # Preserved st_mtime
    
  • shutil.copytree()

    递归地复制源目录树,返回目标目录。


  • 网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接