在此python脚本中,我对服务器进行REST调用,并将信息提取到JSON文件中。使用以下代码,我将4个JSON文件输出到一个文件夹中。我只想输出1个json文件以及所有需要的信息,我该怎么做? 如果那不可能,那么我认为合并4个JSON文件将是我唯一的选择?任何帮助将不胜感激。
from subprocess import call
import os
import json
from glob import glob
fileName = "IP.txt"
file = open(fileName, "r")
for str in file:
login_info = str.split(':')
ip = login_info[0]
username = login_info[1]
password = login_info[2]
os.mkdir(ip)
call(["ilorest", "login", ip, '-u',
username, '-p', password])
call(["ilorest", "save", '--
selector=ComputerSystem', '--json', '-f',
ip+"\\"+ip+".json"])
call(["ilorest", "save", '--
selector=Memory', '--json', '-f',
ip+"\\"+ip+"_memory"+".json"])
call(["ilorest", "save", '--
selector=Processor', '--json', '-f',
ip+"\\"+ip+"_processor"+".json"])
call(["ilorest", "save", '--
selector=HPESmartStorageDiskDrive', '--
json', '-f',
ip+"\\"+ip+"_drives"+".json"])
call(["ilorest", "logout"])
paths = glob('*/')
for d in paths:
print(os.dir(d))
答案 0 :(得分:1)
只需对所有json对象做一个字典,然后将其转储:
types = ['', '_memory', '_processor', '_drives']
result = {}
for suffix in types:
with open(os.path.join(ip, ip + suffix + '.json')) as f:
result[suffix.strip('_')] = json.load(f)
with open(os.path.join(ip, ip+ '_all.json'), 'w') as fw:
json.dump(result, fw)