Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Python
def errorgenerator():
    #Generates error messages and submits to the support team
    counter = open ("reports/counter.txt")
    counternum = int(counter.read())
    counternum = counternum + 1
    counter.close()


    counter = open ("reports/counter.txt", "w+")
    counter.write(int(counternum))
    counter.close()

    print("Apologies, we can not solve this issue. Please contact the supplier, to do this please leave your email to be further contacted by the supplier")
    email = input()

    print("Thank you, your error code is " +
    counter = open (("reports/report-" + int(counternum) + ".txt"), "w+")
    counter.write(email)
    counter.close() )

This code is supposed to create an error code with counternum + 1 and then write that in a file alongside the user's email. For some reason, the code will not recognise the 'counter' in counter.write(email). Can anyone tell me why or how to fix it?

What I have tried:

I have tried writing the counternums as str and int but to no avail. Those were the only things I could think to change.
Posted
Updated 2-Sep-16 19:09pm
v2
Comments
Richard Deeming 2-Sep-16 11:50am    
Are you sure it's not the email variable that's not being recognised?

You cannot write an int to a text stream this way. You should convert it to a string and then reconvert it on input like:
Python
counter.write(str(counternum))

//
num = counter.read()
counternum = int(num) + 1
// write the updated value

Also, do not use "w+" when writing the updated value or you will just end up with a file of sequential numbers. You may find it better to read and write in binary: see 16.2. io — Core tools for working with streams — Python 3.3.6 documentation[^].
 
Share this answer
 
Hi,
I'd recommend you check your indentation well. It's a common error in Python programming.

I can't see the "counter.write(email)" expression in your code.

Regards
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900