Why is they a error. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is they a error.

I am trying to do a Note pad project. But there is an error. The error is io.UnsupportedOperation. Here is the code. file = open("Text_Stuff.txt") print("Welcome to Note pad\n") print("Here you can put text in your Note pad\n") while True: input1 = input("Please enter what do you want to do? See/Edit/Delete ") if input1 == "See": print(file.read()) elif input1 == "Edit": input2 = input("\nPlease enter what do you want to put in the Note pad ") file.write(input2) print("\nYou have successfully rewrite the content of Note pad") elif input1 == "Delete": file.write("") print("\nYou have successfully delete all of the content of Note pad") else: print("\nUmm I think you type the command")

27th Jun 2022, 9:32 AM
Alvin Phyo Htet
Alvin Phyo Htet - avatar
2 Answers
+ 3
Alvin Phyo Htet , there are 2 other issues with the code: -> when a file was opened and used, we have to close the file before the program is terminated. otherwise we may loose the file content. -> the while loop is an infinite loop, so we can not terminate the programm in a controlled manner. we need to implement an "end" option by using a break statement.
27th Jun 2022, 11:51 AM
Lothar
Lothar - avatar
0
When you open file , it is opened in read only and text mode by default. Write operation will give you the above error. If you want to open it in read and write mode , use "a+" mode (i.e. open("Text_Stuff.txt", "a+")) More on what a+ and other modes do, https://www.programiz.com/JUMP_LINK__&&__python__&&__JUMP_LINK-programming/file-operation#:~:text=Opening%20Files%20in%20Python,or%20modify%20the%20file%20accordingly.&text=We%20can%20specify%20the%20mode,append%20a%20to%20the%20file.
27th Jun 2022, 10:32 AM
Abhay
Abhay - avatar