0Pricing
Python Academy · Lesson

Running Commands with subprocess

Execute external programs.

What Is subprocess?

The subprocess module lets your Python program run external commands and other programs, just like typing them in a terminal.

import subprocess

result = subprocess.run(['echo', 'hello'])
print('return code:', result.returncode)

subprocess.run Basics

subprocess.run() is the recommended entry point. You pass the command as a list of arguments: the program name first, then each argument separately.

import subprocess

result = subprocess.run(['echo', 'one', 'two'])
print('done with code', result.returncode)

All lessons in this course

  1. Running Commands with subprocess
  2. Capturing Output
  3. Environment and os Module
  4. shutil for File Operations
← Back to Python Academy