2

Consider:

>>> a = os.popen('chcp 65001')
>>> a.read()
'Active code page: 65001\n'
>>> a.close()
>>> a = os.popen('chcp')
>>> a.read()
'Active code page: 437\n'
>>> a.close()

After I set the code page to 65001, the next time I call CHCP it should say the active code page is 65001, not 437. I tried this in the Windows command prompt and it worked.

Why doesn't it work through Python code?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
russo
  • 271
  • 2
  • 3
  • 9
  • Why are you trying to change the code page in python instead of using [string encodings](http://docs.python.org/library/codecs.html#standard-encodings)? – Seth Aug 26 '10 at 16:46

1 Answers1

3

The reason is that every time you call os.popen you are spawning a new process. Try opening up two cmd.exe sessions and running chcp 65001 in one and chcp in the other—that's what you are doing here in your Python code.

One thing to note: all of the popen*() calls are deprecated as of Python 2.6. The new module to use is the subprocess module.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sean Vieira
  • 155,703
  • 32
  • 311
  • 293