python中copytree的用法_Python shutil.copytree()用法及代码示例

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值