我有一个名为/ AAA的目录,在其中有几个名为AAA_YYMMDD的文件夹,在其中有名为AAA_YYMMDD.xxx的文件,其中xxx是一些不同的扩展名。我正在努力寻找并调整将执行以下操作的所有python代码:
目标目录为/ BBB,包含名为AAA_YYMMDD的子文件夹,与子文件夹相匹配,但每个AAA_YYMMDD子文件夹中都有一个名为“ analysis”的附加子文件夹。
目标:在AAA根目录中找到所有具有AAA_YYMMDD.xx1(一个特定扩展名)的文件,然后将其复制到目标/ BBB目录,但仅在适当的AAA_YYMMDD子文件夹中复制文件是不够的,我希望复制这些文件放在/ AAA_YYMMDD / analysis子文件夹中。
我发现了How to move files based on their names to specific directories in python? 我以为可以通过一些修改使我到达那里。但是我没有处理。
import os
import shutil
destination = r"\\xxx\yyy"
target = r"\\xxx\yyy"
destination_list = os.listdir(destination)
data_dir_list = os.listdir(target)
for dirpath, dnames, fnames in os.walk(destination):
if not os.path.isdir(os.path.join(destination, fnames)):
for prefix in data_dir_list:
if fnames.endswith(".zzz"):
shutil.copy(os.path.join(destination, fnames), os.path.join(target, prefix, fnames))
print "done copying .zzz files"
(根据要求添加了一些借来的代码)