【问题标题】:How can you add written stuff to a file in python [duplicate]如何在python中将书面内容添加到文件中[重复]
【发布时间】:2016-01-30 04:38:10
【问题描述】:

这是我创建的游戏。它应该为文件添加一个新的分数,但它只是覆盖它,我不知道如何修复它。

    input("HIT ENTER WHEN YOU ARE READY")



    #Makes the while Statment repeating it selve
    c= True

    #Repeating it selve
    while c == True:

      import time

      One = time.time()
      a=input("Type as fast the alphabet (Either Caps or all small letters!):  ")
      b= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
      d= 'abcdefghijklmnopqrstuvwxyz'



      if a == b or a == d:
        Two = time.time()
        difference = int(Two - One)
        print("Well done your time is",difference,"seconds")
        c = False
      else:
        print('Arg Wrong the time is still running!')
        a=input(" ")
        C = True




    #Writes the sore to a file
    "Score.txt"

    def writeToFile():
      global myFile

    difference = str(difference)
    myFile = open("Score.txt","w")
    myFile.write(difference + '\n')
    myFile.close()

【问题讨论】:

  • 将文件打开模式从w(写入)更改为a(附加):myFile = open("Score.txt","a")

标签: python


【解决方案1】:

当您以w 作为模式打开文件时,您会覆盖它。

您应该使用a 打开文件,将文本附加到文件末尾。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多