如何在Python中复制文件? [英] How do I copy a file in python?
本文介绍了如何在Python中复制文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Python中复制文件?我找不到 os
。
解决方案
shutil
有很多方法可以使用。其中之一是:
from shutil import copyfile
copyfile(src,dst)
将名为 src
的文件的内容复制到一个文件名为 dst
。目标位置必须是可写的;否则会引发 IOError
异常。如果 dst
已经存在,它将被替换。特殊文件(如字符或块设备和管道)不能使用此功能进行复制。 src
和 dst
是以字符串形式给出的路径名称。
How do I copy a file in Python? I couldn't find anything under os
.
解决方案
shutil
has many methods you can use. One of which is:
from shutil import copyfile
copyfile(src, dst)
Copy the contents of the file named src
to a file named dst
. The destination location must be writable; otherwise, an IOError
exception will be raised. If dst
already exists, it will be replaced. Special files such as character or block devices and pipes cannot be copied with this function. src
and dst
are path names given as strings.
这篇关于如何在Python中复制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文