gpt4 book ai didi

python - 获取文件夹和文件的 JSON 树(但仅限于包含给定字符串的文件)

转载 作者:行者123 更新时间:2023-11-28 22:46:18 24 4
gpt4 key购买 nike

我需要得到一种 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com