I have searched this site for similar issues, but have not found any solutions that worked, hence, this question.
I am writing a Python 3.4 program in which I have a function, export, that essentially appends data to a text file.
The function checks to make sure that there is an appropriate file, if not, it creates one, and then it gets the contents of the file, adds the addendum, and overwrites the file.
Python is throwing the error at for line in file: Also, upon running this program again, once the text file is already created, this error doesn't occur.
Here is the function:
def export(addendum, user):
filename = user + '.txt'
try:
file = open(filename, 'r')
except OSError:
file = open(filename, 'w')
export(addendum, user)
file_contents = ''
print('What day is it? (1-5)')
day = input()
day = int(day)
if day >= 1 and day <= 5:
for line in file:
file_contents += line
file = open(filename, 'w')
new_file = file_contents + '\n' + addendum
file.write(new_file)
file.close()
else:
print('Invalid weekday number...')
sys.exit()