Question: When I intentionally shut down a web sockets server the client JavaScript code:

```
try
{
    self.socket.send('ping');
}
catch(ex)
{
    console.log('exception');
}

```

writes in Chrome console:

```
WebSocket is already in CLOSING or CLOSED state.

```

This exception couldn't be caught by try-catch. How to detect for sure that data could not be sent through web socket? onclose event is not being fired.

Answer: 
`print()` takes separate arguments, `file.write()` *does not*. You can reuse `print()` to 
write to your file instead:

```
with open('result.txt', 'a', encoding='utf-8') as outf:
    for k in main_dic:
        s = 0
        print('stem:', k, file=outf)
        print('word forms and frequencies:', file=outf)
        for w in main_dic[k]:
            print('%-10s ==> %10d' % (w,word_forms[w]), file=outf)
            s += word_forms[w]
        print ('stem total frequency:', s, file=outf)
        print ('------------------------------')

```

I also used the built-in `open()`, there is no need to use the older and far less versatile `codecs.open()` in Python 3. You don't need to call `.keys()` either, looping over the dictionary directly also works.
```
file.write('stem:', k)

```

You're supplying two arguments to `write` on this line, when it only wants one. In contrast, `print` is happy to take as many arguments as you care to give it. Try:

```
file.write('stem: ' + str(k))

```