Python under PowerShell adds characters

eryk sun eryksun at gmail.com
Wed Mar 29 13:22:45 EDT 2017


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


More information about the Python-list mailing list