I haben die folgenden drei Python Scripts:Der Unterschied zwischen bash und Python Rohre
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")
Wenn ich parent1.py mit aufrufen:
python3 parent1.py
es gibt mir wie erwartet die folgende Ausgabe:
This text was created with child.py
wenn ich parent2.py rufen mit:
python3 child.py | python3 parent2.py
i die gleiche Leistung erhalten. Aber im ersten Beispiel bekomme ich die Ausgabe von child.py als Bytes und in der zweiten bekomme ich es direkt als String. Warum ist das? Ist es nur ein Unterschied zwischen Python und Bash Pipes oder gibt es etwas was ich sonst tun könnte, um dies zu vermeiden?
[try this] (http://stackoverflow.com/questions/3999114/linux-pipe-into-python-ncurses-script- stdin-and-termios? answertab = Stimmen # tab-top) – scott