Unicode fixes, file leaks and PEP-8 formatting

This is a bit of a combination of things:

* Removal of  which uses buffer on unicode types. Instead, we now iterate over 32768 code points and encode them to utf-8 individually before writing to file.
* Fixes a number of instances where files were being opened without the statement and without a try/finally to close them.
* Addresses a number of unsafe uses of mutable method parameters, whitespace and general PEP-8 warnings.

The change looks far bigger than it is, because of the whitespace and PEP-8 fixes. I hope you don't mind that.