欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

遇到文件已存在的情况,如何使用shutil.copytree的替代方法来创建文件?

最编程 2024-08-09 13:35:35
...

创建函数:

    '''
    首先需要引入 os shutil模块
    dir1是原目录
    '''
    def copy_dir(self, dir1, dir2):
        dlist = os.listdir(dir1)
        if not os.path.exists(dir2):
            os.mkdir(dir2)
        for f in dlist:
            file1 = os.path.join(dir1, f)  # 源文件
            file2 = os.path.join(dir2, f)  # 目标文件
            if os.path.isfile(file1):
                shutil.copyfile(file1, file2)
            if os.path.isdir(file1):
                self.copy_dir(file1, file2)

推荐阅读