簡體   English   中英

Python:json會刪除詞匯文件數據,而不是編寫

[英]Python: json erasing vocabulary file data instead of writing

您好,感謝您的光臨。 我有一個問題,從GUI寫入了填充有數據的JSON字典:執行此腳本后,即使有一些東西,我也得到了一個空的.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)

在IDLE完成腳本的.json部分后,我再次得到了一個空文件,盡管分別調用變量test_conf_vocabtest_conf_file_w可以正常工作。 我知道我不知何故把json.dump()弄亂了,我只是看不到我自己的任何體面的錯誤,也不用Google json.dump()任何我自己的體面錯誤。

您沒有正確關閉test_conf_file_w文件對象。 下次在處理文件時考慮使用with

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM