Error opening file using utf-8 codec in python

I am executing the following code for windows xp and python 2.6.4

But it shows an IOError.

How to open a file whose name is utf-8 codec.

>>> open( unicode('ํ•œ๊ธ€.txt', 'euc-kr').encode('utf-8') )

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    open( unicode('ํ•œ๊ธ€.txt', 'euc-kr').encode('utf-8') )
IOError: [Errno 22] invalid mode ('r') or filename: '\xed\x95\x9c\xea\xb8\x80.txt'

But the following code is for normal operation.

>>> open( unicode('ํ•œ๊ธ€.txt', 'euc-kr') )
<open file u'\ud55c\uae00.txt', mode 'r' at 0x01DD63E0>
+3
source share
1 answer

The C environment interface that Windows provides Python uses the system code page to encode file names. Unlike OS X and modern versions of Linux, on Windows the system codepage is never UTF-8. So a byte string of UTF-8 will not be good.

, .encode('mbcs'), , , .encode('cp949'). , UTF-8, sys.getfilesystemencoding, utf-8 mbcs Windows.

cp949 , - ( EUC-KR).

, - Unicode. Windows Unicode Windows UTF-16LE, . ( . PEP277.

, : Linux OS X Unicode UTF-8 . Python , Python 3 ( Unicode).

, Unicode Python 2, :

,

open(unicode('ํ•œ๊ธ€.txt', 'euc-kr'))

, 'cp949' ( Windows EUC-KR). , , 'mbcs', , , -, , . , PyShell, , , , :

open(u'ํ•œ๊ธ€')
+16

Source: https://habr.com/ru/post/1722911/


All Articles