python 如何快速复制一个文件,有哪些模块可以使用?

python 如何快速复制一个文件,有哪些模块可以使用?,第1张

概述python 如何快速复制一个文件,有哪些模块可以使用?

在python中,如何复制文件?有哪些模块可以使用?

shutil 模组

os 模组

subprocess 模组

1. 使用shutil模块复制文件

┌──────────────────┬────────┬───────────┬───────┬────────────────┐│     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       │└──────────────────┴────────┴───────────┴───────┴────────────────┘

shutil.copyfile 

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

shutil.copy  复制时不设置元数据

shutil.copy(src_file, follow_symlinks=True)# exampleshutil.copy('source.txt', 'destination.txt')

shutil.copy2 保留元数据进行复制

shutil.copy2(src_file, follow_symlinks=True)# exampleshutil.copy2('source.txt', 'destination.txt')

shutil.copyfileobj

shutil.copyfileobj(src_file_object, dest_file_object[, length])# examplefile_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)

上述方法中,一般是copy2(src,dst)比copyfile(src,dst)更有用,原因如下:

它允许dst将一个目录(而不是完整的目标文件名),在这种情况下,基本名称的src用于创建新的文件;

它将原始修改和访问信息(mtime和atime)保留在文件元数据中(但是,这会带来一些开销)。

简单实例代码:

import shutilshutil.copy2('/src/dir/file.ext', '/dst/dir/newname.ext') # 需要给出完整的目标文件名shutil.copy2('/src/file.ext', '/dst/dir') # 可以给相对路径,目标路径是 /dst/dir/file.ext

2. 使用os模块复制文件

os.popen

# example# In Unix/linuxos.popen('cp source.txt destination.txt') # In windowsos.popen('copy source.txt destination.txt')

os.system

# In linux/Unixos.system('cp source.txt destination.txt')  # In windowsos.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/Unixstatus = subprocess.call('cp source.txt destination.txt', shell=True) # In windowsstatus = subprocess.call('copy source.txt destination.txt', shell=True)

subprocess.check_output 

subprocess.check_output(args, shell=False, universal_newlines=False)# example (WARNING: setting `shell=True` might be a security-risk)# In linux/Unixstatus = subprocess.check_output('cp source.txt destination.txt', shell=True)# In windowsstatus = subprocess.check_output('copy source.txt destination.txt', shell=True)

在python3.5以后,可以用以下代码对小文件(如文本文件,小的图片)进行复制

from pathlib import Pathsource = Path('../path/to/my/file.txt')destination = Path('../path/where/i/want/to/store/it.txt')destination.write_bytes(source.read_bytes())


总结

以上是内存溢出为你收集整理的python 如何快速复制一个文件,有哪些模块可以使用?全部内容,希望文章能够帮你解决python 如何快速复制一个文件,有哪些模块可以使用?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1198105.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-03
下一篇2022-06-03

发表评论

登录后才能评论

评论列表(0条)

    保存