<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>

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

        Writing to a new file if it doesn#39;t exist, and appending to a file if it does(如果新文件不存在则写入新文件,如果存在则追加到文件)

              <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()
                  

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                  Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                  python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                  Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                  How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                  Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)

                      <bdo id='Mc9Ds'></bdo><ul id='Mc9Ds'></ul>
                        <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>

                        • <small id='Mc9Ds'></small><noframes id='Mc9Ds'>

                              <tbody id='Mc9Ds'></tbody>