Courses/Systems Concepts (read-only)

๐Ÿง  Systems Concepts (read-only)

subprocess โ€” shell out safely

Run external commands and capture output.

subprocess.run is the modern, safe entry point. Pass a **list** of args (no shell=True) to avoid shell injection.

import subprocess
r = subprocess.run(
    ["echo", "hello"],
    capture_output=True, text=True, check=True,
)
print(r.stdout.strip())  # hello

**Patterns**

  • check=True raises on non-zero exit โ€” fail fast.
  • capture_output=True, text=True for string stdout/stderr.
  • Long output โ†’ subprocess.Popen with streaming reads.

> ๐Ÿงช Concept-only here โ€” the browser sandbox can't spawn processes.

Concept lesson

This topic relies on OS features the in-browser Python sandbox can't run (threads, subprocesses, sockets). Read the examples here, then try them in real CPython on your machine.

Sign in to track your progress across lessons.