Hi, I am new to python and recently experimenting using pycharm. I am trying to build a basic program that allows me to enter various client details and associated data, stores the input into multiple lists, convert all lists into a single job records list using the zip function and then writes the full list to a text file to be accessed at a later stage.
So far the program does what I want with one key unplanned error:
upon rerunning my python code and accessing the txt file. All previous data written to the txt file is gone and the code re writes from line 1 in the txt file rather than from the next new line.
any help much appreciated.
#Lists to store client data
client_name_list = []
invoice_date_list = []
invoice_number_list = []
amount_due_list = []
material_cost_list = []
net_profit_list = []
#Define a function for appending user input into the relevant lists
def add_new_records():
invoice_number_list.append("Invoice Number: " + input("Enter Invoice Number: "))
invoice_date_list.append("Invoice Date: " + input("Enter Invoice Date: "))
client_name_list.append("Client Name: " + input("Enter Client name: "))
amount_due_list.append("Amount Due: " + input("Enter Amount Due: "))
material_cost_list.append("Material Cost: " + input("Enter Material Cost: "))
net_profit_list.append("Net Profit: " + input("Enter Net Profit: "))
#Create a zip of all client data lists
alldata = zip(invoice_number_list, invoice_date_list, client_name_list, amount_due_list, material_cost_list,
net_profit_list)
# Convert Zip to a List and store as variable
full_list = list(alldata)
#Create a loop so that entries can be inputted for different clients
while int(len(full_list)) < 100:
#Call Function to append the lists
add_new_records()
#Update the Full List for each loop
alldata = zip(invoice_number_list, invoice_date_list, client_name_list, amount_due_list, material_cost_list,
net_profit_list)
full_list = list(alldata)
#Write the inputted client details to a txt document which can be accessed and show all client data.
job_records = open("Job Records.txt", "w")
job_records.write('\n'.join(str(full_list) for full_list in full_list))
job_records.write('\n')
job_records.close()
Hi,
In your line;
job_records = open("Job Records.txt", "w")
The ‘w’ is the write parameter. This overwrites your file each time.
‘a’ is the append parameter. If you swap it to this, it should add to it each time, as you intended.
Hope that helps
1 Like