'Unexpected Type Error' when writing to .txt file

Hi,
When I run this code right from the start of my programm I get no errors:

``` def EdtSlist(self):
 global sellDict 
 print('EdtSlist ' + sellDict['symbol'])
 try:
     file1 = open("Slist.txt", "a")
     file1.write("TEST")
 except:
     print("Unexpected error:", sys.exc_info()[0])
     raise
```

But when I run this code from another method with:

bs.EdtSlist()

I get:

Unexpected error: <class ‘TypeError’>

The line of code that causes the error is:

file1.write(“TEST”)

Does anyone know what is going on here ? Why doesn’t it write text to a txt file after my program has been running for a while ?

Thanks in advance !

Why catch the exception if you’re just going to print out information about it?

What happens if you remove that and show the entire traceback?

Also, this opens a file, but doesn’t close it. Prefer using the context manager to auto-close the file. Can you get this to show the problem as well? Can you include the full traceback?

def EdtSlist(self):
    global sellDict 
    print('EdtSlist ' + sellDict['symbol'])
    with open("Slist.txt", "a") as file1:
        file1.write("TEST")

Thanks for your reply ! Unfortunately your code didn’t write to the txt file but my console didn’t gave any errors. What do you mean with full traceback ?

Are you sure the method is being called? Do you see the print() output from it with the ‘EdtSlist’ string? If it’s getting called, I don’t see how file could not be written. You could add other logging messages inside the context manager just to validate.

The full traceback is the complete error message from python when it exits. Not just one line of it.

Yes the ‘EdtSlist’ is being printed but nothing has been written to the txt file.

 def EdtSlist(self):
     print('EdtSlist ' + sellDict['symbol'])
     try:
         with open("Slist.txt", "a") as file1:
             file1.write("TEST") #<-- type error
     except:
         print("Unexpected error:", sys.exc_info()[0])
         raise

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.

Hi Gerald,

The code you are showing us is only a tiny fragment of the code actually running, and it is not enough for us to see what the error is.

sellDict and bs are not defined. EdtSlist is obviously a method, but a method of what?

The errors you are reporting do not seem to be possible from the method you are showing, so it looks like maybe there is more code involved.

You should start by reading these guides to how to provide a reproducible error:

otherwise we’re just guessing what is going on.

To start with, get rid of this wasteful and pointless try…except block:

try:
file1 = open(“Slist.txt”, “a”)
file1.write(“TEST”)
except:
print(“Unexpected error:”, sys.exc_info()[0])
raise

and any other “try…except:” blocks like it which may catch the exception and suppress the traceback. You, and we, need the traceback to effectively debug the code. I quote:

“You are throwing away the stack trace - a literally priceless body of information that can make the difference between troubleshooting a bug in days, or minutes. Yes, minutes.”

The traceback is the full block of error messages that starts with the word “Traceback” and ends with the final error message, showing all the lines of code involved in the error.

If you don’t see that traceback, you need to hunt down every except: block and remove them with prejudice.

By the way, “Unexpected error” is a kick in the teeth to anyone who uses your software, even if that is only you. No kidding, of course it is an unexpected error, if it were expected you would have done something about it. Don’t tell me it is an unexpected error, tell me what the error is and where it happened – the full traceback – so I can fix it!