Greg
Greg

Reputation: 19

output data collected in a loop to a file each time it loops - Python

def lossFile(eD):
    output = open('lossreport.txt', 'w')
    for i in eD:
        output.write(i + ' ')
    viewReport = input('Would you like to view the data stored in the report? Enter (y/n): \n')
    if viewReport == 'yes' or viewReport =='y':
        outprint = True
        return outprint
    else:
        output.close()
        print('Your data has been successfully been written to the file.')
def readfile(rF):
    if rF:
        file = open('lossreport.txt','r')
        readList = file.readlines()
        file.close()
        for i in readList:
            print(i)

I am writing a program that outputs some basic information to a file. It does this with no problems, it collects a List that stores the username how much they marked down and the date and time it was done.

The problem is the program has a loop that allows the user to work with a new invoice if they please - but when the user enters the loop again to enter these values the data stored in the file is over written. I am trying to get it to show the data for each invoice they choose to work with.

Here is a sample of the file output:

'User: GS Amount Off:(in dollars) 25.00 Date & Time: Wed Jun  2 20:13:13 2021'

Upvotes: 1

Views: 43

Answers (1)

PCM
PCM

Reputation: 3011

Do not use open(lossreport.txt,'w') This will overwrite the file.

You need to append it by using - open(lossreport.txt,'a')

Check this out - File modes

Upvotes: 1

Related Questions