程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python | copy and deletion of files / folders

編輯:Python

This article describes how to use python The file / Folder for copying .

Last Modified: 2022 / 7 / 3


List of articles

  • Feasible Library
  • Copy
    • open
    • os
    • shutil
      • copy2
      • copyfile
    • pathlib
      • write_bytes
  • Reference link


Feasible Library

There are several third-party libraries available for copying files

Library name explain osshutil Several advanced functions are provided to support file copying and deletion .pathlib from Python 3.4 Start , You can use pathlib modular , It provides classes that represent object-oriented file system paths .

It has Path.write_bytes() function , It opens the file pointed to in byte mode , Write data to , Then close the file . There will be no exchange of file metadata .

If the file already exists in the destination , This will overwrite the file .

Copy

open

Examples can be found here 1

src = 'A.py'
dst = 'B.py'
with open(src, 'r') as f:
data = f.read()
with open(dst, 'w') as f:
f.write(data)
# It can be in reading mode (`r`) Open the source file and put its contents in write mode (`w`) Open the target file .
# Write mode opens the file for writing after truncating the file , If the file does not exist, create one .

os

Examples can be found here 2

  • os.system('copy A B')
os.system('cp A.py Folder/B.py')
# Will be under the current path of A.py Copy to Folder Save as next B.py

shutil

Examples can be found here 21

Library name explain copy2 If dst It's a file name ,src Content and file metadata are copied ;
If dst Is an existing file , It will be src File replacement .
If dst Specify a directory , We will copy src File to directory dst. If a file with the same name already exists in the destination , It will be overwritten .

copy2 You can accept the target directory path and copy the file metadata .
copy() Same as copy2 Functional equivalent , But it cannot retain metadata .copyfile If dst It's a file name ,src The content will be copied, but the metadata will not be copied .
If dst Is an existing file , It will be src File replacement .

copy2

  • shutil.copy2(src, dst, *, follow_symlinks=True)
src = 'A.py'
dst = 'Folder'
# dst = 'Folder/B.py'
shutil.copy2(src=src, dst=dst)
# Will be under the current path of A.py copy to Folder or Folder Save as next B.py

copyfile

  • shutil.copyfile(src, dst, *, follow_symlinks=True)
src = 'A.py'
dst = 'Folder/B.py'
shutil.copyfile(src=src, dst=dst)
# Will be under the current path of A.py copy to Folder Save as next B.py, Do not copy metadata .
# however , If it already exists , It will overwrite the target file .

pathlib

Examples can be found here 1


write_bytes

src = pathlib.Path('A.py')
dst = pathlib.Path('Folder/B.py')
dst.write_bytes(src.read_bytes())


Reference link

%todo:
use Python Nine ways to copy files


  1. stay Python Copy files from ︎︎︎

  2. Python Two simple ways to copy files in ︎︎


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved