Python:json擦除词汇表文件数据而不是写入

Python:json擦除词汇表文件数据而不是写入,python,json,qa,Python,Json,Qa,你好,谢谢你的光临。 在编写JSON dict时,我遇到了一个问题,其中包含来自GUI的数据:在执行该脚本之后,我得到了一个空的.JSON文件,即使它填充了一些内容。谷歌搜索没有任何适用的结果 import easygui import sys from json import load, dump from time import strftime, gmtime def runTest(): # This is not really correct, but it works.

你好,谢谢你的光临。 在编写JSON dict时,我遇到了一个问题,其中包含来自GUI的数据:在执行该脚本之后,我得到了一个空的.JSON文件,即使它填充了一些内容。谷歌搜索没有任何适用的结果

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)

在空闲时完成脚本的.json部分后,我又得到了一个空文件,尽管分别调用变量
test_conf_vocab
test_conf_file_w
效果很好。我知道我不知怎么搞砸了
json.dump()
,我只是看不到或用谷歌搜索我自己的任何错误。

你没有正确关闭
测试配置文件。下次考虑使用文件时使用<代码> >代码> 

你好WRAR,这确实有帮助。我将阅读更多关于使用{with}的文档。谢谢你的教训。