python-3.x 如何将多个用户输入放在一个文本文件中?

huwehgph  于 2022-11-26  发布在  Python
关注(0)|答案(3)|浏览(133)

这是我现在拥有的代码

fname = input(">>Please Enter a file name followed by .txt ")
def writedata():
    i=0
for i in range(3):
    f = open(f"{fname}", 'w')
    stdname = input('>>\tStudent Name: \t')
    marks = input('>>\tMark for exam: \t')
    f.write(stdname)
    f.write("\n")
    f.write(marks)
f.close()

def main():
    writedata()

预期的输出

>> Please Enter a file name, followed by .txt: studentRecord.txt
>> Enter record for student 1 in the format of [1. Name, 2. Mark]:
>>       Student Name: James White
>>       Mark for exam: 100
>> Enter record for student 2 in the format of [1. Name, 2. Mark]:
>>       Student Name: James Brown
>>       Mark for exam: 85
>> Enter record for student 3 in the format of [1. Name, 2. Mark]:
>>       Student Name: James King
>>       Mark for exam: 75
>> Student record writing completed!

我尝试了上面的代码,只得到了文本文件中的最后一个用户输入。我应该从def main()传递文件名,但我不知道怎么做,我一直得到无法到达的错误。有人能帮助我,并解释我做错了什么吗?谢谢你的时间和考虑。

xe55xuns

xe55xuns1#

您正在使用write (w)文件方法,该方法会用您传递的任何新数据覆盖您的文件。您需要使用append (a)文件方法,该方法每次都会追加到您的文件中。
The BSD fopen manpage定义档案方法如下:

The argument mode points to a string beginning with one of the following
 sequences (Additional characters may follow these sequences.):

 ``r''   Open text file for reading.  The stream is positioned at the
         beginning of the file.

 ``r+''  Open for reading and writing.  The stream is positioned at the
         beginning of the file.

 ``w''   Truncate file to zero length or create text file for writing.
         The stream is positioned at the beginning of the file.

 ``w+''  Open for reading and writing.  The file is created if it does not
         exist, otherwise it is truncated.  The stream is positioned at
         the beginning of the file.

 ``a''   Open for writing.  The file is created if it does not exist.  The
         stream is positioned at the end of the file.  Subsequent writes
         to the file will always end up at the then current end of file,
         irrespective of any intervening fseek(3) or similar.

 ``a+''  Open for reading and writing.  The file is created if it does not
         exist.  The stream is positioned at the end of the file.  Subse-
         quent writes to the file will always end up at the then current
         end of file, irrespective of any intervening fseek(3) or similar.

您也可以查看python的文档以获得更多信息:https://docs.python.org/3/library/functions.html#open

vd8tlhqk

vd8tlhqk2#

请注意

f = open(f"{fname}", 'w')

您正在使用w模式,该模式每次都会覆盖文件。请改用a+模式,该模式会附加到文件,并在文件尚不存在时创建该文件。

xghobddn

xghobddn3#

fname = str(input(">> Please Enter a file name, followed by .txt: "))
  f = open(f"{fname}","a+")
  for i in range(1, 4):
      print(f">> Enter record for student {i} in the format of [1. Name, 2. Mark]:")
      stdname = str(input(">>      Student Name: "))
      marks = str(input(">>      Mark for exam: "))
      f.write(stdname)
      f.write("\n")
      f.write(marks)
      f.write("\n")
  print("Student record writing completed!")
  f.close()
def main():
  writedata()
if __name__ == '__main__':
  main()

谢谢你们的帮助!这是我想出来的答案。

相关问题