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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With