Jul-29-2019, 09:36 AM
You can reuse the already opened Pipe.
Here two different approaches:
It prints the data on the screen, when the pipe stdin was closed.
Here two different approaches:
import os import subprocess import io # Low Level approach proc = subprocess.Popen(['cat'], stdin=subprocess.PIPE) proc.stdin.fileno() tw = os.fdopen(proc.stdin.fileno(), 'w', encoding='utf8') tw.write('Hello World\n') tw.flush() proc.stdin.write(b'Hello World bytes\n') proc.stdin.flush() proc.stdin.close() # tw is now also closed. # different approach with io proc = subprocess.Popen(['cat'], stdin=subprocess.PIPE) tw2 = io.TextIOWrapper(proc.stdin, encoding='utf8') tw2.write('Hello ...\n') tw2.flush() proc.stdin.write(b'Bin ...\n') proc.stdin.flush() tw2.close() # will close also proc.stdinThe command cat reads from stdin.
It prints the data on the screen, when the pipe stdin was closed.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!