Subprocess
The `subprocess` module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. It replaces `os.system()`, `os.spawn*()`, and `os.popen*()`. The guide walks through subprocess.run, Capturing Output, Popen for Advanced Control, Shell Injection Prevention, Error Handling & Timeouts. `subprocess.run(["ls", "-l"], capture_output=True, text=True)` runs a command and returns a `CompletedProcess` object. Use `check=True` to raise `CalledProcessError` on non-zero exit. The `args` parameter should be a sequence (not a string) for security. Set `capture_output=True` to capture stdout and stderr. Access via `result.stdout` and `result.stderr`. With `text=True` (or `universal_newlines=True`), output is returned as strings instead of bytes. `result.returncode` gives the exit code. `Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE)` gives fine-grained control. Use `.communicate(input=data)` to interact with the process and wait for completion. Use `.poll()` to check if the process has terminated without blocking. The guide is organized into 5 sections that build on each other, each pairing a prose explanation with runnable code.