I am a beginning python user and have come upon a dilemma. My program is supposed to read and write in a specific file. While the write function works, the read function simply erases all text from the file.
def main():
r = open('C:\\Users\\AM\\Desktop\\cool.txt', 'r')
w = open('C:\\Users\\AM\\Desktop\\cool.txt', 'w')
answer = input('Would you like to read or write? (To leave say "quit")')
if(answer == 'read'):
if(r.mode == 'r'):
contents = r.read()
print(contents)
elif(answer == 'write'):
w.write(input('What would you like to write?'))
w.close()
elif(answer == 'quit'):
quit()
else:
print('Sorry, I do not understand')
main()
Thank you for your time!