Python under PowerShell adds characters

Rob Gaddi rgaddi at highlandtechnology.invalid
Wed Mar 29 14:29:15 EDT 2017

On 03/29/2017 11:23 AM, Chris Angelico wrote:
> On Thu, Mar 30, 2017 at 5:19 AM, eryk sun <eryksun at> 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

Engineer 1: Man, that old DOS shell we keep emulating is just getting 
older and clunkier.

Engineer 2: I know, we should rewrite it.  You know, whole new thing, 
really modernize it.

E1: So, like, bring in bash like everyone else?

E2: No, better.  How about something that integrates with no preexisting 
workflow in the world.

E1: Wait, but what commands would it use?

E2: New ones.

E1: But, then how would it behave?

E2: Totally new.  Never before seen.  New commands, new semantics, whole 
9 yards.  If anyone's ever used it, I don't want to.

E1: I love this plan.

Rob Gaddi, Highland Technology --
Email address domain is currently out of order.  See above to fix.

More information about the Python-list mailing list