Python Forum

Full Version: Can't write to .txt after opening websocket
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone,

When I write to a txt file with this code i get no errors. However after I open a websocket I get a 'type error'
The code I use is:

 def EdtSlist(self): 
     try:
         file1 = open("Slist.txt", "wt")
         test="test"
         file1.write(test) #<--type error
         file1.close()
     except:
         print("Unexpected error:", sys.exc_info()[0])
         raise
Any help is much appreciated because I'm stuck for a while now.
The only thing it says is "type error"? No traceback?

What's the point of the except section in your code?
(May-03-2022, 04:04 AM)bowlofred Wrote: [ -> ]The only thing it says is "type error"? No traceback?

What's the point of the except section in your code?

Except is to catch the error:
This is my console output:

EdtSlist gmtusdt
Unexpected error: <class ‘TypeError’>

-------------
When I run just the ‘EdtSlist’ the text gets written to the txt file but when I open a websocket and try to edit text from there it gives me this error. To make sure it wasn't the websocket I closed it before running the ‘EdtSlist’ method but it wouldn't fix the problem.
file1=open("Slist.txt","a")

gives me:
TypeError: 'str' object is not callable
Please provide full code and full error message.
Remove the try/except. You don't do anything to process the error and all the try/ except does is prevent getting a useful error message. Add a "print(open)" command before trying to open the file. What is the output?