Linux 拨号vps windows公众号手机端

python拷贝文件的方法有哪些

lewis 7年前 (2018-11-21) 阅读数 13 #程序编程
文章标签 python

Python拷贝文件的方法有以下几种:

  1. 使用shutil模块的copy()函数:
import shutil
shutil.copy(source, destination)
  1. 使用shutil模块的copy2()函数:
import shutil
shutil.copy2(source, destination)
  1. 使用shutil模块的copyfile()函数:
import shutil
shutil.copyfile(source, destination)
  1. 使用os模块的open()函数进行逐行复制:
import os
with open(source, 'rb') as fsrc, open(destination, 'wb') as fdst:
for line in fsrc:
fdst.write(line)
  1. 使用os模块的read()和write()函数进行全文复制:
import os
with open(source, 'rb') as fsrc, open(destination, 'wb') as fdst:
fdst.write(fsrc.read())
  1. 使用shutil模块的copytree()函数复制整个目录:
import shutil
shutil.copytree(source, destination)

以上方法根据不同的需求和文件类型可以选择适合的方法进行文件拷贝。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门