Python under PowerShell adds characters

Jay Braun lyngwyst at gmail.com
Wed Mar 29 13:42:25 EDT 2017


On Wednesday, March 29, 2017 at 10:28:58 AM UTC-7, eryk sun wrote:
> On Wed, Mar 29, 2017 at 4:06 PM,  <lyngwyst at gmail.com> wrote:
> > I wrote a Python script, which executed as intended on Linux and
> > from cmd.exe on Windows.  Then, I ran it from the PowerShell
> >command line, all print statements added ^@ after every character.
> 
> ISE is the only command-line environment that's specific to
> PowerShell. Surely you wouldn't be running Python scripts in ISE.
> 
> If powershell.exe is run normally, then it's a console application.
> python.exe would inherit the console handle, and that's the end of its
> interaction with PowerShell. At most PowerShell (or any process that's
> attached to the console) may have set the console to a different
> output codepage via SetConsoleOutputCP or set the mode on the screen
> buffer via SetConsoleMode. As far as I know, neither of these can make
> the console print "^@" as a representation of NUL. It only shows "^@"
> in the input buffer when you type Ctrl+2, which is what most terminals
> do. For example:
> 
>     >>> s = sys.stdin.read(6)
>     spam^@
>     >>> s
>     'spam\x00\n'
>     >>> print(s)
>     spam

I'm not using ISE.  I'm using a pre-edited script, and running it with the python command.

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

j


More information about the Python-list mailing list