Python under PowerShell adds characters

Chris Angelico rosuav at gmail.com
Wed Mar 29 14:09:33 EDT 2017


On Thu, Mar 30, 2017 at 4:42 AM, Jay Braun <lyngwyst at gmail.com> wrote:
> Consider the following simple script named hello.py (Python 2.7):
>
> print "Hello"
>
> If I enter:
>
> python hello.py > out.txt
>
> from cmd.exe I get a 6-character file (characters plus new-line).
>
> from PowerShell I get an extract ^@ character after every character

Sounds like cmd and PS are setting the output encodings differently.
Does the same thing occur with Python 3.6?

Try adding this to your script:

import sys
print(sys.stdout.encoding)

and run it in the same two environments (no redirection needed). Do
they give you the same thing?

My suspicion is that you're running cmd.exe in the default Windows
console, and PowerShell in some different console. But it's hard to be
sure.

ChrisA


More information about the Python-list mailing list