Python under PowerShell adds characters

Chris Angelico rosuav at gmail.com
Wed Mar 29 14:23:20 EDT 2017


On Thu, Mar 30, 2017 at 5:19 AM, eryk sun <eryksun at gmail.com> wrote:
> PowerShell is far more invasive. Instead of giving the child process a
> handle for the file, it gives it a handle for a *pipe*. PowerShell
> reads from the pipe, and like an annoying busybody that no asked for,
> decodes the output as text, processes it (e.g. replacing newlines),
> and writes the processed data to the file. For example:
>
>     PS C:\Temp> $script = "import sys; sys.stdout.buffer.write(b'\n')"
>     PS C:\Temp> python -c $script > test.txt
>     PS C:\Temp> python -c "print(open('test.txt', 'rb').read())"
>     b'\xff\xfe\r\x00\n\x00'
>
> I wrote a single byte, b'\n', but PowerShell decoded it, replaced "\n"
> with "\r\n", and wrote it as UTF-16 with a BOM.

Lolwut?

So PS can't handle binary redirection whatsoever. Fascinating.

ChrisA



More information about the Python-list mailing list