python中copytree的⽤法_pytree()⽤法及代
码⽰例
Shutil模块Python提供了许多对⽂件和⽂件集合进⾏⾼级操作的功能。它属于Python的标准实⽤程序模块。此模块有助于⾃动执⾏⽂件和⽬录的复制和删除过程。
⽤法: pytree(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:
使⽤pytree()将⽂件从源复制到⽬标的⽅法
# Python program to pytree() 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 = pytree(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:
python新手代码及作用
使⽤pytree()使⽤复制⽂件的⽅法py()⽅法。
# Python program to pytree() 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
# py() as parameter
destination = pytree(src, dest, copy_function = py)
# 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

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。