[英]Python: json erasing vocabulary file data instead of writing
Hello and thanks for coming. 您好,感谢您的光临。 I have a problem, writing JSON dict filled with data from the GUI: after execution of this script, i got an empty .json file, even if it was filled with something.
我有一个问题,从GUI写入了填充有数据的JSON字典:执行此脚本后,即使有一些东西,我也得到了一个空的.json文件。 Googled without any applicable result.
谷歌搜索没有任何适用结果。
import easygui
import sys
from json import load, dump
from time import strftime, gmtime
def runTest():
# This is not really correct, but it works.
sys.argv = "Some Text"
date = strftime("%d.%m.%Y", gmtime()) + " " + str(int(strftime("%H", gmtime())) + 4) + strftime(":%M", gmtime())
test_case_area = easygui.buttonbox(
"Welcome to Automation Testing Environment, please, select the testing area name:", "Autotests gentle robot",
("GUI", "EPG", "LCN"))
stb = easygui.buttonbox("Please specify the STB type", "STB",
("something_1", "something_2", "something_3", "something_4", "something_5", "something_6", "something_7"))
platform = easygui.buttonbox("Please specify the {0} platform".format(stb), "{0} - Platform".format(stb),
("something_1", "something_2", "something_3", "something_4", "something_5"))
full_platform = stb + "_" + platform
msg = "Specify {0} parameters".format(full_platform)
title = "{0} version information".format(full_platform)
fieldNames = ["Software STB", "Hardware STB", "Module", "Software module", "Hardware module", "Tester", "Drive"]
fieldValues = easygui.multenterbox(msg, title, fieldNames)
test_conf_file_r = open("{0}:\\BBT2\\Configuration\\CP_PowerState.json".format(fieldValues[6]), "r")
test_conf_vocab = load(test_conf_file_r)
test_conf_vocab["platform"] = full_platform
test_conf_vocab["SW_version"] = fieldValues[0]
test_conf_vocab["HW_version"] = fieldValues[1]
test_conf_vocab["test_name"] = test_case_area
test_conf_vocab["disk"] = fieldValues[6]
test_conf_file_w = open("{0}:\\BBT2\\Configuration\\CP_PowerState.json".format(fieldValues[6]), "w")
dump(test_conf_vocab, test_conf_file_w)
After completing the .json part of the script at IDLE, i got an empty file again, though calling variables test_conf_vocab
and test_conf_file_w
separately works fine. 在IDLE完成脚本的.json部分后,我再次得到了一个空文件,尽管分别调用变量
test_conf_vocab
和test_conf_file_w
可以正常工作。 I know that i'm somehow messed up with json.dump()
, i just can't see or Google any decent mistakes of my own. 我知道我不知何故把
json.dump()
弄乱了,我只是看不到我自己的任何体面的错误,也不用Google json.dump()
任何我自己的体面错误。
You didn't close the test_conf_file_w
file object properly. 您没有正确关闭
test_conf_file_w
文件对象。 Next time consider using with
when working with files. 下次在处理文件时考虑使用
with
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.