```python
if PY2:
    opener = py.std.codecs.open
else:
    opener = open

with open(self.logfile, 'w', encoding='utf-8') as logfile:
    logfile.write(...)

# no need to close

```