Shutil模塊Python提供了許多對文件和文件集合進行高級操作的函數。它屬於Python的標準實用程序模塊。此模塊有助於自動執行文件和目錄的複製和刪除過程。
shutil.copytree()
方法以遞歸方式將以源(src)為根的整個目錄樹複製到目標目錄。由(dst)命名的目標目錄必須不存在。它將在複製期間創建。使用copystat()複製目錄的權限和時間,並使用shutil.copy2()複製單個文件。
用法: shutil.copytree(src, dst, symlinks = False, ignore = None, copy_function = copy2, igonre_dangling_symlinks = False)
參數:
src: A string representing the path of the source directory.
dest: A string representing the path of the destination.
symlinks (optional):This parameter accepts True or False, depending on which the metadata of the original links or linked links will be copied to the new tree.
ignore (optional):If ignore is given, it must be a callable that will receive as its arguments the directory being visited by copytree()
, and a list of its contents, as returned by os.listdir()
.
copy_function (optional):The default value of this parameter is copy2. We can use other copy function like copy()
for this parameter.
igonre_dangling_symlinks (optional):This parameter value when set to True is used to put a silence on the exception raised if the file pointed by the symlink doesn’t exist.
返回值: This method returns a string which represents the path of newly created directory.
範例1:
使用shutil.copytree()
將文件從源複製到目標的方法
# Python program to explain shutil.copytree() method
# importing os module
import os
# importing shutil module
import shutil
# path
path = 'C:/Users / Rajnish / Desktop / GeeksforGeeks'
# List files and directories
# in 'C:/Users / Rajnish / Desktop / GeeksforGeeks'
print("Before copying file:")
print(os.listdir(path))
# Source path
src = 'C:/Users / Rajnish / Desktop / GeeksforGeeks / source'
# Destination path
dest = 'C:/Users / Rajnish / Desktop / GeeksforGeeks / destination'
# Copy the content of
# source to destination
destination = shutil.copytree(src, dest)
# List files and directories
# in "C:/Users / Rajnish / Desktop / GeeksforGeeks"
print("After copying file:")
print(os.listdir(path))
# Print path of newly
# created file
print("Destination path:", destination)
輸出:
Before copying file: ['source'] After copying file: ['destination', 'source'] Destination path:C:/Users/Rajnish/Desktop/GeeksforGeeks/destination
範例2:
使用shutil.copytree()
使用複製文件的方法shutil.copy()
方法。
# Python program to explain shutil.copytree() method
# importing os module
import os
# importing shutil module
import shutil
# path
path = 'C:/Users / Rajnish / Desktop / GeeksforGeeks'
# List files and directories
# in 'C:/Users / Rajnish / Desktop / GeeksforGeeks'
print("Before copying file:")
print(os.listdir(path))
# Source path
src = 'C:/Users / Rajnish / Desktop / GeeksforGeeks / source'
# Destination path
dest = 'C:/Users / Rajnish / Desktop / GeeksforGeeks / destination'
# Copy the content of
# source to destination
# using shutil.copy() as parameter
destination = shutil.copytree(src, dest, copy_function = shutil.copy)
# List files and directories
# in "C:/Users / Rajnish / Desktop / GeeksforGeeks"
print("After copying file:")
print(os.listdir(path))
# Print path of newly
# created file
print("Destination path:", destination)
輸出:
Before copying file: ['source'] After copying file: ['destination', 'source'] Destination path:C:/Users/Rajnish/Desktop/GeeksforGeeks/destination
相關用法
- Python next()用法及代碼示例
- Python os.dup()用法及代碼示例
- Python set()用法及代碼示例
- Python Decimal max()用法及代碼示例
- Python PIL ImageOps.fit()用法及代碼示例
- Python os.rmdir()用法及代碼示例
- Python sympy.det()用法及代碼示例
- Python Decimal min()用法及代碼示例
- Python os.readlink()用法及代碼示例
- Python os.writev()用法及代碼示例
- Python os.readv()用法及代碼示例
- Python PIL RankFilter()用法及代碼示例
- Python os.rename()用法及代碼示例
- Python os.sendfile()用法及代碼示例
注:本文由純淨天空篩選整理自Rajnis09大神的英文原創作品 Python | shutil.copytree() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。