N
N
Nikolay Baranenko2017-03-12 09:00:22
Python
Nikolay Baranenko, 2017-03-12 09:00:22

How to correctly set the code page in os.system (cmd)?

Hello.
I want to execute some commands from Python in cmd

import os
cmd = 'chcp'
os.system(cmd)

as a result
������ ������� ��࠭��: 866
, there should be
Текущая кодовая страница: 866
a way to correct the output of the code page so that the Cyrillic alphabet is displayed?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
res2001, 2017-03-12
@res2001

Not sure if it can be done at all.
As far as I know, python operates on UNICODE characters and sets the console accordingly.
cmd gets along very badly with unicode.
For UTF-8 in cmd, the code page is 65001. Try to specify it in the chcp 65001 call and then specify other necessary commands through the ampersand:
cmd = 'chcp 65001 & echo Russian text& dir /b/s'

F
fdrwitch, 2017-03-12
@fdrwitch

in python 3.6 and 2.7 your example gives the same error:
sh: 1: chcp: not found
32512
if you give a simple cmd = 'mkdir aaaaa' type, then everything passes ....
this is how it is written in the libref documentation ( 16.1) "Execute the command (a string) in a subshell."
There must be something wrong with Windows!!!! Check at least for ord('s') -- should be 1099 !!??

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question