How do I run a shell command from Python?

To run a shell command from Python, you can use the `subprocess` module. This module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.

Keywords: Python, subprocess, shell command, run command, execute command
Description: This example demonstrates how to execute a shell command using Python's subprocess module.

Here is an example of running a simple shell command:

import subprocess # Run a shell command result = subprocess.run(['ls', '-la'], capture_output=True, text=True) # Print the output print("Command output:", result.stdout) print("Error (if any):", result.stderr)

Keywords: Python subprocess shell command run command execute command