溫馨提示×

python拷貝文件的方法有哪些

小億
139
2023-08-14 21:56:50
欄目: 編程語言

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)

以上方法根據不同的需求和文件類型可以選擇適合的方法進行文件拷貝。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女