The difference between bash and python pipes -


i have following 3 python scripts:

parent1.py

import subprocess, os, sys  relpath = os.path.dirname(sys.argv[0]) path = os.path.abspath(relpath) child = subprocess.popen([os.path.join(path, 'child.lisp')], stdout = subprocess.pipe) sys.stdin = child.stdout inp = sys.stdin.read() print(inp.decode()) 

parent2.py:

import sys inp = sys.stdin print(inp) 

child.py:

print("this text created in child.py") 

if call parent1.py with:

python3 parent1.py 

it gives me expected following output:

this text created child.py 

if call parent2.py with:

python3 child.py | python3 parent2.py 

i same output. in first example output of child.py bytes , in second directly string. why this? difference between python , bash pipes or there otherwise avoid this?

when python opens stdin , stdout, detects encoding use , uses text i/o give unicode strings.

but subprocess not (and can not) detect encoding of subprocess start, it'll return bytes. can use io.textiowrapper() instance wrap child.stdout pipe provide unicode data:

sys.stdin = io.textiowrapper(child.stdout, encoding='utf8') 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -