<legend id='PE8zk'><style id='PE8zk'><dir id='PE8zk'><q id='PE8zk'></q></dir></style></legend>
      <i id='PE8zk'><tr id='PE8zk'><dt id='PE8zk'><q id='PE8zk'><span id='PE8zk'><b id='PE8zk'><form id='PE8zk'><ins id='PE8zk'></ins><ul id='PE8zk'></ul><sub id='PE8zk'></sub></form><legend id='PE8zk'></legend><bdo id='PE8zk'><pre id='PE8zk'><center id='PE8zk'></center></pre></bdo></b><th id='PE8zk'></th></span></q></dt></tr></i><div id='PE8zk'><tfoot id='PE8zk'></tfoot><dl id='PE8zk'><fieldset id='PE8zk'></fieldset></dl></div>

      <small id='PE8zk'></small><noframes id='PE8zk'>

    1. <tfoot id='PE8zk'></tfoot>
        <bdo id='PE8zk'></bdo><ul id='PE8zk'></ul>

        如果新文件不存在则写入新文件,如果存在则追加到文件

        时间:2023-09-27

              <tbody id='CwMB4'></tbody>
            <tfoot id='CwMB4'></tfoot>

                <bdo id='CwMB4'></bdo><ul id='CwMB4'></ul>
                <i id='CwMB4'><tr id='CwMB4'><dt id='CwMB4'><q id='CwMB4'><span id='CwMB4'><b id='CwMB4'><form id='CwMB4'><ins id='CwMB4'></ins><ul id='CwMB4'></ul><sub id='CwMB4'></sub></form><legend id='CwMB4'></legend><bdo id='CwMB4'><pre id='CwMB4'><center id='CwMB4'></center></pre></bdo></b><th id='CwMB4'></th></span></q></dt></tr></i><div id='CwMB4'><tfoot id='CwMB4'></tfoot><dl id='CwMB4'><fieldset id='CwMB4'></fieldset></dl></div>
                <legend id='CwMB4'><style id='CwMB4'><dir id='CwMB4'><q id='CwMB4'></q></dir></style></legend>

                <small id='CwMB4'></small><noframes id='CwMB4'>

                • 本文介绍了如果新文件不存在则写入新文件,如果存在则追加到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个程序将用户的 highscore 写入文本文件.该文件由用户在选择 playername 时命名.

                  I have a program which writes a user's highscore to a text file. The file is named by the user when they choose a playername.

                  如果具有该特定用户名的文件已经存在,则程序应附加到该文件(以便您可以看到多个 highscore).如果不存在具有该用户名的文件(例如,如果用户是新用户),它应该创建一个新文件并写入它.

                  If the file with that specific username already exists, then the program should append to the file (so that you can see more than one highscore). And if a file with that username doesn't exist (for example, if the user is new), it should create a new file and write to it.

                  这是相关的,到目前为止还没有工作的代码:

                  Here's the relevant, so far not working, code:

                  try: 
                      with open(player): #player is the varible storing the username input
                          with open(player, 'a') as highscore:
                              highscore.write("Username:", player)
                  
                  except IOError:
                      with open(player + ".txt", 'w') as highscore:
                          highscore.write("Username:", player)
                  

                  如果文件不存在,上面的代码会创建一个新文件,并写入它.如果它存在,则在我检查文件时没有附加任何内容,并且我没有收到任何错误.

                  The above code creates a new file if it doesn't exist, and writes to it. If it exists, nothing has been appended when I check the file, and I get no errors.

                  推荐答案

                  我不清楚你感兴趣的高分到底存储在哪里,但是下面的代码应该是你需要检查的文件存在并在需要时附加到它.我更喜欢这种方法而不是try/except".

                  It's not clear to me exactly where the high-score that you're interested in is stored, but the code below should be what you need to check if the file exists and append to it if desired. I prefer this method to the "try/except".

                  import os
                  player = 'bob'
                  
                  filename = player+'.txt'
                  
                  if os.path.exists(filename):
                      append_write = 'a' # append if already exists
                  else:
                      append_write = 'w' # make a new file if not
                  
                  highscore = open(filename,append_write)
                  highscore.write("Username: " + player + '
                  ')
                  highscore.close()
                  

                  这篇关于如果新文件不存在则写入新文件,如果存在则追加到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python JSON转储/附加到.txt,每个变量都在新行 下一篇:我可以在 Python 中使用前置元素而不是附加来扩展列表吗?

                  相关文章

                      <bdo id='Mc9Ds'></bdo><ul id='Mc9Ds'></ul>
                    1. <legend id='Mc9Ds'><style id='Mc9Ds'><dir id='Mc9Ds'><q id='Mc9Ds'></q></dir></style></legend>
                      <i id='Mc9Ds'><tr id='Mc9Ds'><dt id='Mc9Ds'><q id='Mc9Ds'><span id='Mc9Ds'><b id='Mc9Ds'><form id='Mc9Ds'><ins id='Mc9Ds'></ins><ul id='Mc9Ds'></ul><sub id='Mc9Ds'></sub></form><legend id='Mc9Ds'></legend><bdo id='Mc9Ds'><pre id='Mc9Ds'><center id='Mc9Ds'></center></pre></bdo></b><th id='Mc9Ds'></th></span></q></dt></tr></i><div id='Mc9Ds'><tfoot id='Mc9Ds'></tfoot><dl id='Mc9Ds'><fieldset id='Mc9Ds'></fieldset></dl></div>
                      <tfoot id='Mc9Ds'></tfoot>

                    2. <small id='Mc9Ds'></small><noframes id='Mc9Ds'>