我需要以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
但我很难将其整合到问题顶部的代码中。
我感谢任何提示或建议。
答案 0 :(得分:4)
我不想使用os.walk()
重写您的代码。我会对您进行一些小改动。
密钥是使用None作为修剪文件的sentinel值,使用空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))