将一个文件夹中的内容复制到另一个文件夹中

说明:
文件夹a中是原始数据。包含5个文件夹,每个文件夹中都有一个“.txt”文件。
文件夹b中包含3个空文件夹。
文件夹c中为空文件夹。
在这里插入图片描述
要实现的任务:将b中的文件在a中找到对应的原始数据,然后存放到文件夹c中。

import os
import shutil

a = r'D:\bmi结果\zhedang\a'
b = r'D:\bmi结果\zhedang\b'
c = r'D:\bmi结果\zhedang\c'

for file_b in os.listdir(b):
#     img_path=os.path.join(path,file,name)
    for file_a in os.listdir(a):
        if file_a == file_b:
            path_c = os.path.join(c,file_b)
            path_a = os.path.join(a,file_b)
            if not os.path.exists(path_c):
#                 os.makedirs(path_c)
                shutil.copytree(path_a, path_c)  


print('copy dir finished!')```

执行代码后c文件夹中的内容:
![在这里插入图片描述](https://img-blog.csdnimg.cn/e421c03825fb40b9ada85a91d37a7903.jpeg)

猜你喜欢

转载自blog.csdn.net/weixin_44934373/article/details/125729007