subprocess module

The standard library module for running other programs from Python and collecting their output and exit code.

Building a shell command from user input with shell=True allows command injection.

In more detail

subprocess.run(["git", "status"], capture_output=True, text=True) runs a command and returns its result. Pass the command as a list rather than one string with shell=True, so user input can never be run as extra shell commands.

Example

import subprocess
subprocess.run(["python3", "--version"])

Related terms

Taught in

References

Certification courses