In Python, you can execute a program or call a system command using the subprocess
module. Here’s an example:
For instance, to list files in a directory:
Use shell=True
for complex commands but avoid it for untrusted input to prevent security risks. If you need to capture the command’s output, use subprocess.run
with capture_output=True
: