如何在Python中复制文件?

Mat*_*att 2171 python filesystems copy file file-copying

如何在Python中复制文件?

我找不到任何东西os.

Swa*_*ati 2661

shutil有很多方法可以使用.其中之一是:

from shutil import copyfile

copyfile(src, dst)
Run Code Online (Sandbox Code Playgroud)

将命名文件的内容复制到名为src的文件中dst.目的地位置必须是可写的; 否则,IOError将引发例外.如果dst已经存在,则将被替换.使用此功能无法复制特殊文件,如字符或块设备和管道.src并且dst是以字符串形式给出的路径名.

  • 在副本(src,dst)中,dst可以是目录. (360认同)
  • copy和copyfile有什么区别? (126认同)
  • 请注意,并非所有元数据都将被复制,具体取决于您的平台. (36认同)
  • 注意它不能处理像`~`这样的缩写,但它可以处理相对路径 (9认同)
  • 在 Python 3.8 中,速度得到了一些显着的提升(大约快 50%,具体取决于操作系统)。 (9认同)
  • 请注意,它不是原子操作.在线程应用程序中使用它时要小心. (5认同)
  • @Owen 确实可以,但目录必须已经存在。顺便说一下,只要目标目录已经存在,`dst` 可以以斜杠结尾也可以不以斜杠结尾,这无关紧要。 (3认同)
  • 目标文件夹是否需要存在? (2认同)
  • @KansaiRobot:是的,否则你会得到一个异常:`FileNotFoundError:目录不存在:foo/` (2认同)

jez*_*ael 1027

??????????????????????????????????????????????????????????????????
?     Function     ? Copies ?   Copies  ?Can use?   Destination  ?
?                  ?metadata?permissions?buffer ?may be directory?
??????????????????????????????????????????????????????????????????
?shutil.copy       ?   No   ?    Yes    ?   No  ?      Yes       ?
?shutil.copyfile   ?   No   ?     No    ?   No  ?       No       ?
?shutil.copy2      ?  Yes   ?    Yes    ?   No  ?      Yes       ?
?shutil.copyfileobj?   No   ?     No    ?  Yes  ?       No       ?
??????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

  • 现在这只是糟糕的,糟糕的API设计. (31认同)
  • 请注意,[即使是 `shutil.copy2()` 函数也无法复制所有文件元数据](https://docs.python.org/3/library/shutil.html)。 (13认同)
  • 也可以将该副本和copy2接受目录添加为目标,而不是需要指定完整名称的copyfile. (9认同)
  • “目标目录正常”列表示什么?这是否意味着复制前不检查目标目录是否正确? (4认同)
  • @KatieS“ Dest dir OK”指示是否可以将目录指定为目的地,而不是文件。从`shutil.copy(src,dst,*,follow_symlinks = True)`上的文档(也适用于`copy2`):“如果_dst_指定了目录,则将使用_src_中的基本文件名将文件复制到_dst_中。 ” (4认同)
  • @jezrael从doc中提取:`dst必须是完整的目标文件名; 请查看shutil.copy()以获取接受目标目录路径的副本.(此处:https://docs.python.org/3/library/shutil.html#shutil.copyfile) (3认同)
  • @FakherMokadem-用手;) (2认同)

unm*_*ted 689

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
Run Code Online (Sandbox Code Playgroud)

  • 我试图从100万个文件中随机复制100k文件.`copyfile`比`copy2`快得多 (18认同)
  • 我是否正确地假设``shutil.copy2('/ dir/file.ext','/ new/dir /')``(在目标路径后用斜线)将消除是否复制到新的模糊性文件名为"dir"或将文件放入该名称的目录中? (4认同)

max*_*zig 108

您可以使用包中的一个复制功能shutil:

??????????????????????????????????????????????????????????????????????????????
Function              preserves     supports          accepts     copies other
                      permissions   directory dest.   file obj    metadata  
??????????????????????????????????????????????????????????????????????????????
shutil.copy              ?             ?                 ?           ?
shutil.copy2             ?             ?                 ?           ?
shutil.copyfile          ?             ?                 ?           ?
shutil.copyfileobj       ?             ?                 ?           ?
??????????????????????????????????????????????????????????????????????????????

例:

import shutil
shutil.copy('/etc/hostname', '/var/tmp/testhostname')
Run Code Online (Sandbox Code Playgroud)

  • @lightalchemist我只是将vim用作便签本,从Wikipedia表中复制了使用的unicode符号,然后将结果复制到了stackoverflow编辑器中以进行最终润饰。 (12认同)
  • 只是很好奇,你是如何产生那张桌子的? (7认同)
  • 这与两年前的其他答案有何不同?/sf/answers/2125151591/ (3认同)
  • 好。YMMV,但是我认为,对外观上的更改和类似的较小改进最好作为对现有答案的编辑,而不是重复答案。 (2认同)

pi.*_*pi. 91

复制文件是一个相对简单的操作,如下面的示例所示,但您应该使用shutil stdlib模块.

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)
Run Code Online (Sandbox Code Playgroud)

如果你想通过文件名复制,你可以这样做:

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)
Run Code Online (Sandbox Code Playgroud)

  • 前一段时间我注意到模块被称为shutil(单数)而不是shutils(复数),实际上它在Python 2.3中也是_is_.不过我把这个功能留在这里作为例子. (24认同)
  • 复制文件的*内容*是一项简单的操作.使用元数据复制文件绝不是直截了当的,如果您想要跨平台,则更是如此. (3认同)
  • 真正.查看shutil文档,copyfile函数也不会复制元数据. (2认同)
  • 是的,我不确定为什么你不会只复制`shutil.copyfileobj`的来源.此外,您没有任何`try,finally`来处理异常后关闭文件.但是,我会说,你的函数根本不应该负责打开和关闭文件.这应该放在一个包装器函数中,就像`shutil.copyfile`包装`shutil.copyfileobj`一样. (2认同)

kma*_*o23 82

在Python中,您可以使用复制文件


import os
import shutil
import subprocess
Run Code Online (Sandbox Code Playgroud)

1)使用shutil模块复制文件

shutil.copyfile 签名

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

# example    
shutil.copyfile('source.txt', 'destination.txt')
Run Code Online (Sandbox Code Playgroud)

shutil.copy 签名

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

# example
shutil.copy('source.txt', 'destination.txt')
Run Code Online (Sandbox Code Playgroud)

shutil.copy2 签名

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

# example
shutil.copy2('source.txt', 'destination.txt')  
Run Code Online (Sandbox Code Playgroud)

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)  
Run Code Online (Sandbox Code Playgroud)

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')
Run Code Online (Sandbox Code Playgroud)

os.system 签名

os.system(command)


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

# In Windows
os.system('copy source.txt destination.txt')
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

  • 使用单字符串命令是不好的编码风格(灵活性,可靠性和安全性),请尽可能使用[['copy',sourcefile,destfile]]语法,尤其是当参数来自用户输入时。 (7认同)
  • 为什么您列出了常规复制功能的许多不良选择? (6认同)
  • 只有第一个例子回答了这个问题。运行 shell 命令不可移植,也不能真正执行操作。 (6认同)
  • shutil是内置的,无需提供非便携式替代品。通过删除与系统有关的解决方案,可以真正改善答案,在删除之后,此答案仅是现有答案的副本/文档的副本。 (4认同)
  • 感谢您列出的许多选项。恕我直言,“os.popen('cp source.txt destination.txt')”(及其类似)是糟糕的设计。Python 是一种独立于平台的语言,使用这样的代码就会破坏这个伟大的功能。 (4认同)
  • os.popen已经过时了一段时间。和`check_output`不返回状态,而是返回输出(在`copy / cp`情况下为空) (2认同)
  • Shutil 实际上并不复制文件。[文档顶部有一个很大的警告](https://docs.python.org/3/library/shutil.html)。“这意味着文件所有者和组以及 ACL 都会丢失。在 Mac OS 上,不使用资源分支和其他元数据。这意味着资源将丢失,文件类型和创建者代码将不正确。在 Windows 上,文件所有者、ACL 和备用数据流不会被复制。” (2认同)

Air*_*Ltd 64

使用shutil模块.

copyfile(src, dst)
Run Code Online (Sandbox Code Playgroud)

将名为src的文件的内容复制到名为dst的文件中.目的地位置必须是可写的; 否则,将引发IOError异常.如果dst已经存在,它将被替换.使用此功能无法复制特殊文件,如字符或块设备和管道.src和dst是以字符串形式给出的路径名.

查看filesys,了解标准Python模块中可用的所有文件和目录处理函数.


Noa*_*nos 44

目录和文件复制示例 - 来自Tim Golden的Python Stuff:

http://timgolden.me.uk/python/win32_how_do_i/copy-a-file.html

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"
Run Code Online (Sandbox Code Playgroud)


Rit*_*ger 22

在 Python 中复制文件有两种最佳方法。

\n

1.我们可以使用该shutil模块

\n

代码示例:

\n
import shutil\nshutil.copyfile(\'/path/to/file\', \'/path/to/new/file\')\n
Run Code Online (Sandbox Code Playgroud)\n

除了copyfile之外,还有其他方法可用,例如 copy、copy2 等,但就性能而言,copyfile是最好的,

\n

2.我们可以使用OS模块

\n

代码示例:

\n
import os\nos.system(\'cp /path/to/file /path/to/new/file\')\n
Run Code Online (Sandbox Code Playgroud)\n

另一种方法是使用子进程,但它并不可取,因为它\xe2\x80\x99 是调用方法之一并且不安全。

\n


Alg*_*bra 16

首先,我制作了详尽的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',]
}
Run Code Online (Sandbox Code Playgroud)

其次,解释exmaples中的复制方法:

  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)
Run Code Online (Sandbox Code Playgroud)
  1. 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
Run Code Online (Sandbox Code Playgroud)
  1. 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
Run Code Online (Sandbox Code Playgroud)
  1. 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)
# Preseved st_mtime
Run Code Online (Sandbox Code Playgroud)
  1. `shutil.copytree()``

递归复制以src为根的整个目录树,返回目标目录


yel*_*w01 14

我建议使用Swati的答案,但假设你有一个文本文件,并且不想在你的代码中使用额外的库来复制它,你可以使用以下单行:

with open(source, 'r') as src, open(dest, 'w') as dst: dst.write(src.read())
Run Code Online (Sandbox Code Playgroud)

  • 这会在写回之前将完整的源文件读入内存.因此,除了最小的文件复制操作之外,这会不必要地浪费内存. (3认同)

小智 13

你可以用 os.system('cp nameoffilegeneratedbyprogram /otherdirectory/')

或者就像我做的那样,

os.system('cp '+ rawfile + ' rawdata.dat')
Run Code Online (Sandbox Code Playgroud)

rawfile我在程序中生成的名称在哪里.

这是一个仅限Linux的解决方案

  • 这不是便携式的,因为你只需要使用shutil就不必要了. (10认同)
  • 即使`shutil`不可用 - `subprocess.run()`(没有`shell = True`!)是`os.system()`的更好的替代品. (4认同)
  • 更便携 (2认同)
  • 在调用外部程序时,@maxschlepzig 建议的 `subprocess.run()` 是向前迈出的一大步。然而,为了灵活性和安全性,使用 `['cp', rawfile, 'rawdata.dat']` 形式传递命令行。(但是,对于复制,建议 `shutil` 和朋友调用外部程序。) (2认同)
  • 尝试使用带有空格的文件名。 (2认同)

ytp*_*lai 10

对于大文件,我所做的是逐行读取文件并将每行读入数组.然后,一旦阵列达到一定大小,将其附加到新文件.

for line in open("file.txt", "r"):
    list.append(line)
    if len(list) == 1000000: 
        output.writelines(list)
        del list[:]
Run Code Online (Sandbox Code Playgroud)

  • 这似乎有点多余,因为编写器应该处理缓冲。对于open('file.txt','r')中的l:output.write(l)应该可以找到;只需根据您的需要设置输出流缓冲区即可。或者你可以通过遍历`output.write(read(n)); output.flush()`其中n是您想一次写入的字节数。这两个都没有条件检查哪个是奖励。 (2认同)
  • 糟透了 它没有充分的理由就做不必要的工作。它不适用于任意文件。如果输入在Windows之类的系统上具有异常行尾,则副本不是字节相同的。为什么您认为这比在`shutil`中调用复制函数更容易理解?即使忽略“ shutil”,一个简单的块读/写循环(使用无缓冲IO)也是很简单的,它会很高效,并且比这有意义得多,因此肯定更容易教和理解。 (2认同)

dee*_*ive 10

from subprocess import call
call("cp -p <file> <file>", shell=True)
Run Code Online (Sandbox Code Playgroud)

  • 这取决于平台,所以我不会使用. (10认同)
  • 这种"呼叫"是不安全的.请参考有关它的子进程文档. (5认同)
  • 这不是可移植的,并且是不必要的,因为您只能使用shutil。 (2认同)
  • 嗯,为什么Python,那么? (2认同)

S47*_*471 10

open(destination, 'wb').write(open(source, 'rb').read())
Run Code Online (Sandbox Code Playgroud)

以读取模式打开源文件,并以写入模式写入目标文件。

  • 这不是在所有这些 `open(...)` 上都缺少 `.close()` 吗? (3认同)
  • 这个想法很好,代码也很漂亮,但是适当的 copy() 函数可以做更多的事情,例如复制属性(+x 位),或者例如在发现磁盘已满情况时删除已经复制的字节. (2认同)
  • **所有**答案都需要解释,即使只是一句话。没有任何解释开创了不好的先例,并且无助于理解该程序。如果一个彻底的 Python 菜鸟看到了这个,想使用它,但因为不理解而不能使用怎么办?您希望您的答案对所有人都有帮助。 (2认同)
  • 与 [yellow01 的答案] (/sf/answers/3198594241/) 相同的次优内存浪费方法。 (2认同)

Bas*_*asj 6

这是一个简单的方法,没有任何模块。它类似于这个答案,但如果它是一个不适合 RAM 的大文件,它也有好处:

with open('sourcefile', 'rb') as f, open('destfile', 'wb') as g:
    while True:
        block = f.read(16*1024*1024)  # work by blocks of 16 MB
        if not block:  # end of file
            break
        g.write(block)
Run Code Online (Sandbox Code Playgroud)

由于我们正在编写一个新文件,它不会保留修改时间等。如果需要,
我们可以使用os.utime它。


Leo*_*chi 6

万一你已经走到了这一步。答案是你需要完整的路径和文件名

import os

shutil.copy(os.path.join(old_dir, file), os.path.join(new_dir, file))
Run Code Online (Sandbox Code Playgroud)


Mar*_*arc 5

Python 3.5开始,您可以对小文件(例如:文本文件,小jpegs)执行以下操作:

from pathlib import Path

source = Path('../path/to/my/file.txt')
destination = Path('../path/where/i/want/to/store/it.txt')
destination.write_bytes(source.read_bytes())
Run Code Online (Sandbox Code Playgroud)

write_bytes 将覆盖目的地位置的所有内容

  • 至少它不会一遍又一遍地重复相同的解决方案。 (3认同)
  • 然后有人在大文件上(有意或有意地)使用了代码…使用`shutil`中的函数可以为您处理所有特殊情况,使您省心。 (2认同)
  • @Kevin因为这会加载内存中的所有内容。 (2认同)