Python UTF-8 print fails when redirecting stdout

Consider the following piece of code:

# -*- coding: utf-8 -*-
print u"Վարդանաշեն"

Running this in a terminal works:

$ python test.py
Վարդանաշեն

Redirecting standard output to a file fails:

$ python test.py > file
Traceback (most recent call last):
  File "test.py", line 2, in <module>
    print u"Վարդանաշեն"
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-9: ordinal not in range(128)

Explanations are available on Python official wiki: default encoding has to be forced.

With an environment variable:

$ PYTHONIOENCODING='utf_8'
$ export PYTHONIOENCODING
$ python test.py > file
$

With source modification:

import sys
import codecs
import locale
sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)

#python, #encoding, #shell - Posted in the Dev category