首页 > 解决方案 > 将多个文件名与同一目录中名称的前缀进行比较

问题描述

我在同一目录中有多个 .png 和 .json 文件。我想检查目录中可用的文件是否同名或不喜欢 a.png & a.json、b.png & b.json

标签: pythonscripting

解决方案


你可以试试这个:

import os

_, _ ,files = os.walk('.').next()
json = [f[:-5] for f in files if f.endswith('.json')]
png  = [f[:-4] for f in files if f.endswith('.png')]

json_only = set(json) - set(png)
png_only = set(png) - set(json)

json_and_png = set(json) & set(png)

... etc...

推荐阅读