作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要得到一种 JSON 格式的过滤目录/文件结构。
具体来说,我需要只包含包含给定字符串的文件,并且只包含包含此类文件的目录(在它们本身或它们的某些后代中)。
这段代码:
import os
import json
def path_to_dict(path):
d = {'name': os.path.basename(path)}
if os.path.isdir(path):
d['type'] = "directory"
d['children'] = [path_to_dict(os.path.join(path,x)) for x in os.listdir\
(path)]
else:
d['type'] = "file"
return d
print json.dumps(path_to_dict('.'), indent=2)
给我所有目录和文件的漂亮 JSON 树,从当前目录开始,以我想要的格式:
{
"type": "directory",
"name": ".",
"children": [
{
"type": "file",
"name": "attribute_container.c"
},
{
"type": "file",
"name": "node.c"
},
{
"type": "directory",
"name": "power",
"children": [
{
"type": "file",
"name": "clock_ops.c"
},
{
"type": "file",
"name": "common.c"
},
{
"type": "file",
"name": "domain.c"
},
{
"type": "file",
"name": "domain_governor.c"
},
{
"type": "file",
"name": "generic_ops.c"
},
{
"type": "file",
"name": "wakeup.c"
}
]
},
{
"type": "directory",
"name": "regmap",
"children": [
{
"type": "file",
"name": "internal.h"
},
{
"type": "file",
"name": "Kconfig"
},
{
"type": "file",
"name": "Makefile"
},
{
"type": "file",
"name": "regcache-flat.c"
},
{
"type": "file",
"name": "regmap-spmi.c"
},
{
"type": "file",
"name": "regmap.c"
}
]
},
{
"type": "file",
"name": "soc.c"
},
{
"type": "file",
"name": "syscore.c"
},
{
"type": "file",
"name": "topology.c"
},
{
"type": "file",
"name": "transport_class.c"
} ] }
但是,我只需要包含给定字符串的文件。此外,只有包含此类文件的文件夹或它们的某些后代包含此类文件。 (可以这么说,我需要一种“修剪”)
我知道在文件中查找字符串的解决方案:
my_file = ...
my_string = ...
infile = open(my_file,"r")
numlines = 0
found = 0
for line in infile:
numlines += 1
found += line.count(my_string)
infile.close()
print "%s was found %i times in %i lines", %string, %found, %numlines
但我很难将它集成到问题顶部的代码中。
我很感激任何提示或建议。
最佳答案
我不想使用 os.walk()
重写您的代码。我只会对您的代码做一些小改动。
关键是使用 None 作为标记值来修剪文件,并使用空 children
列表来修剪目录。该实现写得不好,但它向您展示了如何使用测试的核心。
import os
import json
def check_in_file(my_file,my_string):
with open(my_file) as f:
try:
return my_string in f.read()
except:
return False
def path_to_dict(path, my_string=None):
d = {'name': os.path.basename(path)}
if os.path.isdir(path):
d['type'] = "directory"
d['children'] = []
paths = [os.path.join(path,x) for x in os.listdir(path)]
#Just the children that contains at least a valid file
for p in paths:
c = path_to_dict(p, my_string)
if c is not None:
d['children'].append(c)
if not d['children']:
return None
else:
if my_string is not None and not check_in_file(path,my_string):
return None
d['type'] = "file"
return d
print(json.dumps(path_to_dict('.',), indent=2))
print(json.dumps(path_to_dict('.','kkkkk'), indent=2))
关于python - 获取文件夹和文件的 JSON 树(但仅限于包含给定字符串的文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27661247/
我是一名优秀的程序员,十分优秀!