Python Glossary Русский

all Python 3subprocessterm

subprocess.PIPE

constants

The constant that redirects stdin/stdout/stderr into a pipe. It lets the program read the output or write the input itself.

Syntax

subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Inside subprocess.run() you rarely need to name PIPE at all — capture_output=True does exactly this for both streams. PIPE earns its keep when you want to route streams: stderr=subprocess.STDOUT merges errors into the same stdout, while subprocess.DEVNULL throws unwanted output away instead of buffering it. A pipe has a finite buffer, so whatever you send into one someone has to read, or the child blocks forever on its next write.

Examples

import subprocess
result = subprocess.run(
['echo', 'привет'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
text=True
)
print(result.stdout)   # привет
print(result.stderr)   # пусто

See also

Open in the full glossarydocs.python.orgReport an inaccuracy