problem while using os.walk with utf-8 characters

Thierry GAYET thierry.gayet at gmail.com
Tue May 12 11:39:49 EDT 2015


Hi,

I'm using the os.walk function for parsing files from an external mass-storage such as usbkey.

When i try the following code, i have an error:

from __future__ import unicode_literals

import sys
reload(sys)
sys.setdefaultencoding('utf_8')

for dirname, dirnames, filenames in os.walk(os.path.join(massStorage, path)):
or
for dirname, dirnames, filenames in os.walk(unicode(path, 'utf-8')

And my error is:
'ascii' codec can't encode character u'\xe9' in position 58: ordinal not in range(128)

The wrong character is an accent (french one).

It doesn't work when i start it but after a reboot i need to restart the code to make it work properly without any error.

Any idea about the first fact related to the usf-8 support with external files that can be encoded with any charset (latin-1, utf-8, ... )

Secondly, why it can works after a restart of the python script ?

BR

Thierry GAYET






More information about the Python-list mailing list