Note:
Folder a contains raw data. Contains 5 folders, each folder has a ".txt" file.
Folder b contains 3 empty folders.
There is an empty folder in folder c.
The task to be realized: Find the corresponding original data of the file in b in a, and then store it in folder 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)