Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 OS walk unicode exception

Tags:

python

unicode

I'm using python 3.3.3 in win7 - I just want to list all the files in a network directory.

import os

for root, dirs, files in os.walk("X:\\network\\path\\foo\\bar\\baz"):
    print(root)
    print(dirs)
    print(files)

After a while of printing stuff it outputs this exception.

Traceback (most recent call last):
  File "program.py", line 6, in <module>
    print(files)
  File "C:\Python33\lib\encodings\cp437.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2019' in position
2753: character maps to <undefined>

How do I get this to print simply? I always seem to have these unicode problems with python 3. I just want simple things be simple.

like image 378
MKaras Avatar asked May 31 '26 09:05

MKaras


1 Answers

In Windows 7, the console doesn't properly support Unicode encodings. You need to encode your strings to cp-437:

print(root.encode("cp437", "backslashreplace").decode("cp437"))

That should remove all the characters unprintable in the DOS console and replace them with their \unnnn or \xnn equivalent.

like image 69
Tim Pietzcker Avatar answered Jun 02 '26 23:06

Tim Pietzcker