image_dict = {} # handle name and link
all_file = list(Path().iterdir()) # get all file in current directory
填充image_dict
for f in all_file:
extension = str(f).split(".")[-1]
filename = str(f).split("." + extension)[0]
if extension in ["png", "jpeg"]: # In case of there is other file that's no an image
part_name = filename.split("_") # Get keywords from filename
for pn in part_name: # Append a link from each keyword
if image_dict.get(pn, None) is not None:
image_dict[pn].append(f.resolve())
else:
image_dict[pn] = [f.resolve()]
print(image_dict) # This is the result
如果您只想根据“\ux”分割文件名, 如果 filename=“animal\u cat\u kowai.png”
这将导致您的_lisr=[“动物”、“猫”、“kowai”]
我认为
list
不适合链接。相反,使用dict
首先导入所需的模块
启动变量
填充
image_dict
相关问题 更多 >
编程相关推荐