0

Executing the following python program

test.py:

# -*- coding: utf-8 -*- 

print "Hühnchen"

hexdump:

00000000  23 20 2d 2a 2d 20 63 6f  64 69 6e 67 3a 20 75 74  |# -*- coding: ut|
00000010  66 2d 38 20 2d 2a 2d 20  0a 0a 70 72 69 6e 74 20  |f-8 -*- ..print |
00000020  22 48 c3 bc 68 6e 63 68  65 6e 22 0a              |"H..hnchen".|

from the command prompt in Windows 7 with code page 65001 and the Lucida Console font causes an IOError:

$ python test.py
HühnchenTraceback (most recent call last):
  File "test.py", line 3, in <module>
    print "Hühnchen"
IOError: [Errno 2] No such file or directory

To exclude any side effects of my Windows installation I reproduced the problem in a fresh virtual machine with the following steps:

  1. Install Windows 7 Ultimate SP1 Build 7601 (SHA1 of the ISO: 36ae90defbad9d9539e649b193ae573b77a71c83) in a virtual machine
  2. Install python 2.7.13 64-bit
  3. Open cmd.exe
  4. Set the font to Lucida Console
  5. Change the code page to 65001 to support UTF-8
  6. Execute the above script (make sure that the file encoding is UTF-8)

With the same result: enter image description here

What is happening here?

user1658887
  • 449
  • 4
  • 15

1 Answers1

0

So the only way I could recreate this error was to change to Lucida and run the chcp 65001 command before running the program. According to this post, 'Prior to Windows 8, output using codepage 65001 is also broken'[1]. If you want to just print "Hühnchen" or any characters with from UTF-8 I think the best solution is to not do so from the command line as it always seems to mess up the characters. When I ran the same code on PyCharm CE it worked fine and even when I opened the python 2.7 command prompt it still printed properly. It seems to be a very niche problem you're experiencing which should be able to be worked around.

[1] chcp 65001 codepage results in program termination without any error

Community
  • 1
  • 1
TLOwater
  • 638
  • 3
  • 13