加载时出现文件IO错误

时间:2013-09-01 23:03:02

标签: python json file-io python-3.x

def load():
    with open("random_number_highscores.txt","r") as x:
        print ("HIGHSCORES")
        print ("Least guesses made.")
        print (json.load(x))
        time.sleep(1)

def save(a):
    with open("random_number_highscores.txt", "a") as x:
        json.dump(a, x)
    print ("saved.")
    time.sleep(1)

为什么def加载不起作用。 我尝试用json.dump(str(a),x)保存,但它不起作用或者只是得到错误

1 个答案:

答案 0 :(得分:1)

追加到保存文件,你需要改写:

def save(a):
    with open("random_number_highscores.txt", "w") as x:
        json.dump(a, x)

json.load()代码会遇到多个JSON值,并且无法处理文件中的多个。