抱歉说我有:
directory A at the path = /Users/John/Desktop/test_1
目录A中的文件夹结构看起来是这样的:
./fashion
./.DS_Store
./fashion/clothes
./fashion/.DS_Store
./fashion/2.jpg
./fashion/3.jpg
./fashion/1.jpg
./fashion/clothes/sneakers
./fashion/clothes/.DS_Store
./fashion/clothes/sneakers/2.jpg
./fashion/clothes/sneakers/3.jpg
./fashion/clothes/sneakers/1.jpgdirectory B at the path = /Users/John/Desktop/test_2
目录B中的文件夹结构如下所示:
./fashion
./.DS_Store
./fashion/clothes
./fashion/.DS_Store
./fashion/clothes/sneakers
./fashion/clothes/high_heels
./fashion/clothes/.DS_Store我试图根据文件夹名称匹配将文件从目录A复制到目录B,这样,在目录B中,您可以看到下面的内容。如果目录B中不存在folder_name,则不要复制图像。
编辑:文件路径不一定匹配,只是如果(目录A中文件夹的名称) == (目录B中文件夹的名称),将文件夹A的内容复制到文件夹B中,否则,什么也不做。
fashion: img1, img2
fashion > clothes: img1, img2
fashion > clothes > sneakers: img1, img2
fashion > clothes > high-heels: {}再次感谢您!
发布于 2019-08-01 21:51:11
你可以试一试:
import shutil
import os
path_a = '/Users/John/Desktop/test_1'
path_b ='/Users/John/Desktop/test_2'
for root, dirs, files in os.walk(path_b):
similar_path = root.replace(path_b, path_a)
if os.path.exists(similar_path):
for file_path in (os.path.abspath(x) for x in os.listdir(similar_path) if os.path.isfile(x)):
print(f'File: {file_path} moved to --->> {root}')
shutil.copy(file_path, root)发布于 2019-08-01 21:52:48
https://stackoverflow.com/questions/57317332
复制相似问题