How to read special characters from stdin in Python?
        Posted  
        
            by erickrf
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by erickrf
        
        
        
        Published on 2010-05-23T18:40:42Z
        Indexed on 
            2010/05/23
            18:51 UTC
        
        
        Read the original article
        Hit count: 481
        
I'm having trouble reading special characters from stdin.
Here are my attempts:
import os
dir = raw_input("Dir name: ")
Dir name: c:/á
os.chdir(dir)
WindowsError: [Error 2] The system cannot find the file specified: 'c:/\x81\xe1'
Ok, so I tried to get the default system encoding and recode the string from stdin:
import locale
encoding = locale.getdefaultlocale()[1]
print encoding
cp1252
unicode(dir, encoding)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\Python26\lib\encodings\cp1252.py", line 15, in decode
    return codecs.charmap_decode(input,errors,decoding_table)
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 3: character maps to <undefined>
Now, I don't know how to solve this. Nor can I understand - why is there a problem when I try to access a directory with a name written in the system default encoding itself??
© Stack Overflow or respective owner