3

I want to parse to output of windows commands such as ipconfig, netstat, tasklist etc. using python.

But certain part (Fixed) of output of these commands is returned based on current locale/codepage.

E.g. in ipconfig output

the string           IP Address..........:123.23.242.12
will be in chinese   IP 地址   ..........:123.23.242.12

or in other language depending on codepage.

Is there any way of executing commands such as os.popen("ipconfig") with specific locale/codepage setting.

  1. I tried using locale module: locale.setlocale(locale.LC_ALL,"american_united states") locale.getlocale() returns "English_United States.1252" but os.system("chcp") still returns old codepage: 936 and os.popen("ifconfig") returns strings in chinese.

  2. os.system("chcp 65001") now os.popen("ifconfig") returns english strings but it clears all the previous output from console(may be it opens new console) which is not acceptable.

Is there any way of executing commands such as os.popen("ipconfig") with specific locale/codepage setting? Any help is appreciated.

glglgl
  • 89,107
  • 13
  • 149
  • 217
Rakesh
  • 271
  • 1
  • 2
  • 11
  • I was unable to reproduce this on my Win7 Professional... I seem to always get English strings. What version of Windows are you using? – immortal Aug 26 '11 at 10:37
  • I am using windows 7 only but installed chinese OS. – Rakesh Aug 26 '11 at 10:43
  • I believe your problem is in the fact that `setlocale` works on the local process and the new configuration is not propagated into the child process created by `popen`. This is a rather tough one to deal with in Windows... In *nix systems you could set the environment in the command line: `PATH=$PATH:/home/foo ./my_program` or use `os.exec*` to leave the current environment to a child process, but I can't find a way to do the same with Windows. – immortal Aug 26 '11 at 10:51
  • I just end up use windows api to get all infomation i need.Parse command line output is not working in windows. – bronze man Oct 26 '17 at 13:26

0 Answers0