Again on Python and unicode

Yes, there are numerous good articles on the subject, but still every time I am stumbled upon infamous

UnicodeEncodeError: 'charmap' codec can't encode character u'\u0306' in position 50: character maps to <undefined>

and I have to research it again. Somehow it does not stick. Perhaps because it just should have worked “out of box”. Anyway. Hereby a small recipe that “shuts the unicode nagging” in Python 2 when enumerating all the files:

import sys
import codecs

sys.stdout = codecs.getwriter('utf-8')(sys.stdout, 'strict')
for root, folders, files in os.walk(unicode(path), 'utf-8'):
    print u'%s\r' % root,
    <do whatever you want with the files></div>