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)
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)
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
78
┌──────────────────┬───────────────┬──────────────────┬──────────────┬───────────┐
│ Function │Copies metadata│Copies permissions│Can use buffer│Dest dir OK│
├──────────────────┼───────────────┼──────────────────┼──────────────┼───────────┤
│shutil.copy │ No │ Yes │ No │ Yes │
│shutil.copyfile │ No │ No │ No │ No │
│shutil.copy2 │ Yes │ Yes │ No │ Yes │
│shutil.copyfileobj│ No │ No │ Yes │ No │
└──────────────────┴───────────────┴──────────────────┴──────────────┴───────────┘
591
在Python中,您可以使用复制文件
shutil 模块
os 模块
subprocess 模块
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')
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.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)
8
我建议使用Swati's答案,但假设你有一个 text file 并且不想在你的代码中使用额外的库只是为了复制它,你可以使用以下单行:
with open(source, 'r') as src, open(dest, 'w') as dst: dst.write(src.read())
14 回答
首先,我制作了详尽的shutil方法备忘单供您参考 .
其次,解释exmaples中的复制方法:
递归复制以src为根的整个目录树,返回目标目录
目录和文件复制示例 - 来自Tim Golden的Python Stuff:
http://timgolden.me.uk/python/win32_how_do_i/copy-a-file.html
对于大文件,我所做的是逐行读取文件并将每行读入数组 . 然后,一旦数组达到一定大小,将其附加到新文件 .
复制文件是一个相对简单的操作,如下面的示例所示,但您应该使用shutil stdlib module .
如果你想通过文件名复制,你可以这样做:
您可以使用shutil包中的一个复制功能:
例:
copy2(src,dst)通常比copyfile(src,dst)更有用,因为:
它允许
dst
成为目录(而不是完整的目标文件名),在这种情况下basename的basename用于创建新文件;它保留了文件元数据中的原始修改和访问信息(mtime和atime)(但是,这会带来轻微的开销) .
这是一个简短的例子:
在Python中,您可以使用复制文件
shutil 模块
os 模块
subprocess 模块
1)使用shutil模块复制文件
shutil.copyfile 签名
shutil.copy 签名
shutil.copy2 签名
shutil.copyfileobj 签名
2)使用os模块复制文件
os.popen 签名
os.system 签名
3)使用子进程模块复制文件
subprocess.call 签名
subprocess.check_output 签名
我建议使用Swati's答案,但假设你有一个 text file 并且不想在你的代码中使用额外的库只是为了复制它,你可以使用以下单行:
看模块 shutil . 它包含函数 copyfile (src,dst)
shutil有很多方法可以使用 . 其中之一是:
将名为
src
的文件的内容复制到名为dst
的文件中 . 目的地位置必须是可写的;否则,将引发IOError
例外 . 如果dst
已存在,则将替换它 . 使用此功能无法复制特殊文件,如字符或块设备和管道 .src
和dst
是以字符串形式给出的路径名 .你可以用
os.system('cp nameoffilegeneratedbyprogram /otherdirectory/')
或者就像我做的那样,
其中
rawfile
是我在程序中生成的名称 .这是一个仅限Linux的解决方案
使用shutil module .
将名为src的文件的内容复制到名为dst的文件中 . 目的地位置必须是可写的;否则,将引发IOError异常 . 如果dst已经存在,它将被替换 . 使用此功能无法复制特殊文件,如字符或块设备和管道 . src和dst是以字符串形式给出的路径名 .
查看filesys,了解标准Python模块中可用的所有文件和目录处理函数 .