Tôi có ba kịch bản python sau:Sự khác biệt giữa bash và trăn Ống
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")
Nếu tôi gọi parent1.py bằng:
python3 parent1.py
nó mang lại cho tôi như dự kiến đầu ra sau đây:
This text was created with child.py
nếu tôi gọi parent2.py với:
python3 child.py | python3 parent2.py
tôi nhận được đầu ra tương tự. Nhưng trong ví dụ đầu tiên tôi nhận được kết quả đầu ra của child.py dưới dạng byte và trong phần thứ hai tôi lấy nó trực tiếp dưới dạng một chuỗi. Tại sao điều này? Nó chỉ là một sự khác biệt giữa python và bash ống hoặc là có một cái gì đó tôi có thể làm khác để tránh điều này?
[thử này] (http://stackoverflow.com/questions/3999114/linux-pipe-into-python-ncurses-script- stdin-and-termios? answerertab = phiếu # tab-top) – scott