Popen.communicate()
Popen.communicate() is a Python method in the Subprocess Module category. Sends data to stdin and reads from stdout/stderr until EOF. The syntax is Popen.communicate(input=None, timeout=None). Returns: (stdout, stderr) tuple. A typical example: p = subprocess.Popen(["sort"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out, _ = p.communicate(b"3\n1\n2\n")
print(out) # b"1\n2\n3\n". A close sibling is subprocess.run(), which runs a command and waits for it to complete. Returns a CompletedProcess instance. A close sibling is subprocess.Popen(), which executes a child program in a new process (lower-level than run()). A close sibling is subprocess.call(), which runs a command and returns its return code (legacy API, use run() instead). A close sibling is subprocess.check_output(), which runs a command and returns its output as bytes. Raises CalledProcessError on failure. More about this category: Subprocess management — run, Popen, call, PIPE, CalledProcessError. For a broader walkthrough, read the Subprocess Module guide. Related Subprocess Module entries: subprocess.run(), subprocess.Popen(), subprocess.call(), subprocess.check_output(), subprocess.check_call(), subprocess.PIPE.