ho le seguenti tre script python:La differenza tra i tubi bash e Python
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 was created in child.py")
Se chiamo genitore1.py con:
python3 parent1.py
Mi dà come previsto il seguente output:
This text was created with child.py
se chiamo parent2.py con:
python3 child.py | python3 parent2.py
io ottenere lo stesso risultato. Ma nel primo esempio ottengo l'output di child.py come byte e nel secondo lo prendo direttamente come una stringa. Perchè è questo? E 'solo una differenza tra Python e bash pipe o c'è qualcosa che potrei fare diversamente per evitare questo?
[provare questo] (http://stackoverflow.com/questions/3999114/linux-pipe-into-python-ncurses-script- stdin-and-termios? answertab = voti # tab-top) – scott