dict嵌套list
运行代码:
import glob import os project_dict = {} pdl_pdf = glob.glob(r"C:\Users\86177\Desktop\testbed\server\PDL1\one.pdf")[0] HE_jpg = glob.glob(r"C:\Users\86177\Desktop\testbed\server\PDL1\HE.jpg")[0] IHC_jpg = glob.glob(r"C:\Users\86177\Desktop\testbed\server\PDL1\IHC.jpg")[0] if pdl_pdf and HE_jpg and IHC_jpg: project_dict["pdl"] = [] for path in [pdl_pdf, HE_jpg, IHC_jpg]: path_name = os.path.basename(path) # 得到最后的文件名 project_dict["pdl"].append({"name": path_name, "path": path}) print(project_dict)
输出结果:
{'pdl': [{'name': 'one.pdf', 'path': 'C:\\Users\\86177\\Desktop\\testbed\\server\\PDL1\\one.pdf'}, {'name': 'HE.jpg', 'path': 'C:\\Users\\86177\\Desktop\\testbed\\server\\PDL1\\HE.jpg'}, {'name': 'IHC.jpg', 'path': 'C:\\Users\\86177\\Desktop\\testbed\\server\\PDL1\\IHC.jpg'}]}