import subprocess cmd = ['python', 'child.py'] with subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc: proc.stdin.write(b'Hello world\n') proc.stdin.close() output, error = proc.communicate() print(output.decode())
import subprocess cmd = ['python', 'child.py'] with subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc: output = proc.stdout.read() proc.stdout.close() error = proc.stderr.read() proc.stderr.close() print(output.decode())In this example, we read the output from the child process using proc.stdout.read() method, before closing the output channel using proc.stdout.close(). We then retrieve any error messages from the child process using proc.stderr.read() method, before closing the error channel using proc.stderr.close(). Finally, we print the output from the child process to the console. Package/Library: Subprocess (part of the Python Standard Library)