将多个文件名与同一目录中的名称前缀进行比较 [英] Compare multiple file name with the prefix of name in same directory

查看:28
本文介绍了将多个文件名与同一目录中的名称前缀进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一目录中有多个 .png 和 .json 文件.我想检查目录中可用文件的名称是否相同,例如 a.png &a.json, b.png &b.json

I have multiple .png and .json file in same directory . And I want to check where the files available in the directory are of same name or not like a.png & a.json, b.png & b.json

推荐答案

你可以试试这个:

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...

这篇关于将多个文件名与同一目录中的名称前缀进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆