如何使用 Python 创建现有文件的重复文件?


shutil 模块提供复制文件和整个文件夹的函数。

调用 shutil.copy(source, destination) 将把路径 source 处的文件复制到路径 destination 处的文件夹中。(source 和 destination 都是字符串。)如果 destination 是文件名,它将用作复制文件的名称。此函数返回复制文件的路径字符串。例如,

>>> import shutil
>>> # Copy the file in same folder with different name
>>> shutil.copy('original.txt', 'duplicate.txt')
'/home/username/duplicate.txt'
>>> shutil.copy('original.txt', 'my_folder/duplicate.txt')
'/home/username/my_folder/duplicate.txt'

更新于:2020 年 2 月 17 日

971 次浏览

开始您的 职业

通过完成课程获得认证

开始学习
广告
© . All rights reserved.