내가 가지고있는 다음과 같은 세 가지 파이썬 스크립트 :배쉬와 파이썬 파이프의 차이
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")
만약 내가 parent1.py를 다음과 같이 호출하면 :
python3 parent1.py
는 예상 다음과 같은 출력처럼 날을 제공합니다
This text was created with child.py
내가 가진 parent2.py 호출하는 경우 :
python3 child.py | python3 parent2.py
을 내가 같은 출력을 얻을. 하지만 첫 번째 예제에서는 바이트로 child.py의 출력을 얻고 두 번째에서는 문자열로 직접 가져옵니다. 왜 이런거야? 그것은 파이썬과 bash pipe 사이의 차이 일까 아니면 이것을 피하기 위해 할 수있는 일이 있습니까?
[이 시도] (http://stackoverflow.com/questions/3999114/linux-pipe-into-python-ncurses-script- : 당신은 유니 코드 데이터를 제공하기 위해
child.stdout
파이프를 포장하는io.TextIOWrapper()
instance을 사용할 수 있습니다 stdin-and-termios? answertab = votes # tab-top) – scott