Home > Back-end >  How to put multiple user inputs in a text file?
How to put multiple user inputs in a text file?

Time:11-22

this is the code I have right now

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

the output that is intended

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

I tried the above code and only got the last user input in the text file. I was supposed to pass file name from def main() but I don't know how to do that, I kept getting unreachable error. Can someone please help me and explain what I'm doing wrong? Thank you for your time and consideration.

CodePudding user response:

Please take note of

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

You are using the w mode, which overwrites the file everytime. Instead, use a mode, which appends to the file, and creates the file if it does not yet exist.

CodePudding user response:

You are using the write (w) file method, which overwrites your file with any new data you pass. You need the append (a) file method, which will append to your file each time.

The BSD fopen manpage defines the file methods as follows:

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.

You could also look at python's documentation for some more information: https://docs.python.org/3/library/functions.html#open

  • Related