import codecs f = codecs.open('foo.txt', 'w', encoding='utf-8') f.write('\xaa')