python - 使用 python 搜索 Json 目录
问题描述
我创建了一个包含特定目录的 Json 文件。我正在尝试编写一些允许用户进出每个“文件夹”的东西,几乎创建了一个命令行“文件资源管理器”
我的 json 文件格式如下:
{
"children": [
{
"children": [
{
"name": "somefile.cmd",
"path": "C:\\some\\directory\\somefile.cmd",
"type": "file"
},
{
"name": "otherfile.ps1",
"path": "C:\\some\\directory\\somefile.ps1",
"type": "file"
},
{
"name": "somefile.exe",
"path": "C:\\some\\directory\\somefile.exe",
"type": "file"
}
],
"name": "somefile",
"path": "C:\\some\\directory",
"type": "folder"
},
{
"children": [
.
.
.
我正在使用的功能
def search_json(filename):
json_file = open(filename)
data = json.load(json_file)
subsyst_count = 1
subsyst_list = []
#list of subsystems
for i in data['children']:
print(subsyst_count, i['name'])
subsyst_list.append(i['name'])
subsyst_count = subsyst_count + 1
user = int(input('Which Subsystem?'))
#search json for children of subsyst_list[user]
print(subsyst_list[user])
for i in data['children']:
if i['name'] == subsyst_list[user]:
print(i['name'])
for j in i['name']:
print(j[0])
我希望它进入第一个子文件夹,计算它下面的所有文件夹,并提示用户选择一个数字来确定要进入的子系统。然后我希望它搜索新的“子”子系统目录并再次编号要进入的文件或要选择的文件。相反,当我有
print(j['name'])
然后当我有时,该函数只是拼写子系统的名称:
print(j['0'])
解决方案
推荐阅读
- ruby-on-rails - 如何正确测试我的 API 创建的条带客户?
- ios - M1 上的 Flutter:GoogleMapsBase 和 Flutter.h 从 Windows 移至 mac 后未发现错误
- c - C getline() 读取最后一行两次
- code-coverage - Jacoco 分支在 try-with-resources 没有捕获后错过
- assembly - 为什么 FASM 能够成功地汇编具有 32 位有效地址和指令“use16”的代码?
- c# - 当我尝试仅使用接口时使用没有接口的 Nuget 包
- python - “NoneType”和“int”错误实例之间不支持“>”的解决方案?
- javascript - 如何让同一个类中的多个按钮调用 JavaScript 中的同一个参数函数?
- ios - SwiftUI List 动画导致应用在绑定快速变化时显示过时的结果
- javascript - 使用模式将文本分成单独的元素 - javascript