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

Redirecting standard output to a file fails:

$ python > file
Traceback (most recent call last):
  File "", 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:

$ python > 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