如何在Python中复制文件?
当前回答
首先,我对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',]
}
其次,举例说明复制方法:
shutil.copyfileobj(fsrc,fdst[,length])操纵打开的对象在[3]中:src='~/Documents/Head+First+SQL.pdf'在[4]中:dst='~/desktop'在[5]中:shutil.copyfileobj(src,dst)AttributeError:“str”对象没有属性“read”#复制文件对象在[7]中:open(src,'rb')为f1,open(os.path.join(dst,'test.pdf'),'wb')为f2:…:shutil.copyfileobj(f1,f2)在[8]中:os.stat(os.path.join(dst,'test.pdf'))输出[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_mime=1516067335,st_ctime=1516087345)shutil.copyfile(src,dst,*,follow_symlinks=True)复制并重命名在[9]中:shutil.copyfile(src,dst)IsADirectoryError:[Erno 21]是一个目录:~/desktop'#所以dst应该是文件名而不是目录名shutil.copy()复制而不预先保存元数据在[10]中:shutil.copy(src,dst)输出[10]:~/desktop/Head+First+SQL.pdf'#检查其元数据在[25]中:os.stat(src)输出[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)在[26]中:os.stat(os.path.join(dst,'Head+First+SQL.pdf'))输出[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=1516086425,st_ctime=151066425)#st_atime、st_mtime、st_ctime已更改shutil.copy2()复制并保留元数据在[30]中:shutil.copy2(src,dst)输出[30]:~/desktop/Head+First+SQL.pdf'在[31]中:os.stat(src)输出[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)在[32]中:os.stat(os.path.join(dst,'Head+First+SQL.pdf'))输出[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=1516087055)#保留时间(_m)shutil.copytree()递归复制以src为根的整个目录树,返回目标目录。
其他回答
这里有一个简单的方法,不需要任何模块。这与这个答案类似,但如果它是一个不适合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)
因为我们正在编写一个新文件,所以它不会保留修改时间等。如果需要,我们可以使用os.utime。
Function | Copies metadata |
Copies permissions |
Uses file object | Destination 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 |
使用subprocess.call复制文件
from subprocess import call
call("cp -p <file> <file>", shell=True)
首先,我对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',]
}
其次,举例说明复制方法:
shutil.copyfileobj(fsrc,fdst[,length])操纵打开的对象在[3]中:src='~/Documents/Head+First+SQL.pdf'在[4]中:dst='~/desktop'在[5]中:shutil.copyfileobj(src,dst)AttributeError:“str”对象没有属性“read”#复制文件对象在[7]中:open(src,'rb')为f1,open(os.path.join(dst,'test.pdf'),'wb')为f2:…:shutil.copyfileobj(f1,f2)在[8]中:os.stat(os.path.join(dst,'test.pdf'))输出[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_mime=1516067335,st_ctime=1516087345)shutil.copyfile(src,dst,*,follow_symlinks=True)复制并重命名在[9]中:shutil.copyfile(src,dst)IsADirectoryError:[Erno 21]是一个目录:~/desktop'#所以dst应该是文件名而不是目录名shutil.copy()复制而不预先保存元数据在[10]中:shutil.copy(src,dst)输出[10]:~/desktop/Head+First+SQL.pdf'#检查其元数据在[25]中:os.stat(src)输出[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)在[26]中:os.stat(os.path.join(dst,'Head+First+SQL.pdf'))输出[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=1516086425,st_ctime=151066425)#st_atime、st_mtime、st_ctime已更改shutil.copy2()复制并保留元数据在[30]中:shutil.copy2(src,dst)输出[30]:~/desktop/Head+First+SQL.pdf'在[31]中:os.stat(src)输出[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)在[32]中:os.stat(os.path.join(dst,'Head+First+SQL.pdf'))输出[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=1516087055)#保留时间(_m)shutil.copytree()递归复制以src为根的整个目录树,返回目标目录。
shutil模块提供了一些对文件的高级操作。它支持文件复制和删除。
请参考下表了解您的用例。
Function | Utilize File Object |
Preserve File Metadata |
Preserve Permissions |
Supports Directory Dest. |
---|---|---|---|---|
shutil.copyfileobj | ✔ | ⅹ | ⅹ | ⅹ |
shutil.copyfile | ⅹ | ⅹ | ⅹ | ⅹ |
shutil.copy2 | ⅹ | ✔ | ✔ | ✔ |
shutil.copy | ⅹ | ⅹ | ✔ | ✔ |
推荐文章
- Numpy Max vs amax vs maximum
- 我应该在.gitignore文件中添加Django迁移文件吗?
- 每n行有熊猫
- 实例属性attribute_name定义在__init__之外
- 如何获取在Python中捕获的异常的名称?
- 第一次出现的值大于现有值的Numpy
- 如何从Python函数中返回两个值?
- 前一个月的Python日期
- Python中方括号括起来的列表和圆括号括起来的列表有什么区别?
- Python日志记录不输出任何东西
- 每n秒运行特定代码
- SQLAlchemy是否有与Django的get_or_create等价的函数?
- 如何将python datetime转换为字符串,具有可读格式的日期?
- 美丽的汤和提取div及其内容的ID
- 在Python中重置生成器对象