GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask
  • Introduction1 min read
  • Input & Output2 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Python subprocess module - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask

Python subprocess module

Last Updated : 4 Apr, 2026

The subprocess module is used to run external programs or system commands from Python. It allows to execute commands, capture their output and interact with other processes. One can also send input, handle errors and check execution status.

Example: In this example, a system command is executed to check the Python version and print its output.

Python
import subprocess
res = subprocess.run(["python", "--version"], capture_output=True, text=True)
print(res.stdout)

Output
Python 3.13.0

Explanation:

  • subprocess.run() executes the command
  • capture_output=True stores output instead of printing directly
  • text=True converts output to string
  • result.stdout contains the output

subprocess.run()

subprocess.run() executes a command and waits until it finishes. It returns an object that contains output, errors and return status of the command.

Example: Here, a command is executed and both output and return code are printed.

Python
import subprocess
res = subprocess.run(["python", "--version"], capture_output=True, text=True)
print("Output:", res.stdout)
print("Return Code:", res.returncode)

Output
Output: Python 3.13.0

Return Code: 0

Explanation:

  • subprocess.run() runs the command and waits
  • res.returncode gives execution status (0 means success)
  • res.stdout stores command output

subprocess.Popen

Popen starts a new process and allows interaction with it while it is running. It gives control over input, output and error streams.

Example: In this example, a process is started and its output is captured.

Python
import subprocess

process = subprocess.Popen(
    ["python", "--version"],
    stdout=subprocess.PIPE,
    text=True
)

output, _ = process.communicate()
print(output)

Output
Python 3.13.0

Explanation:

  • subprocess.Popen() starts the process
  • stdout=subprocess.PIPE captures output
  • process.communicate() reads output and waits for completion

subprocess.check_output

check_output() executes a command and returns only its output. If the command fails, it raises an exception.

Example: Here, a command is executed and its output is directly returned.

Python
import subprocess
try:
    output = subprocess.check_output(["python", "--version"], text=True)
    print(output)
except subprocess.CalledProcessError:
    print("Command failed")

Output
Python 3.13.0

Explanation:

  • subprocess.check_output() runs the command
  • Returns output directly and raises error if execution fails

subprocess.call

subprocess.call() runs a command and returns its exit status. It does not store output in variables.

Example: In this example, a command is executed and its success status is checked.

Python
import subprocess
code = subprocess.call(["python", "--version"])

if code == 0:
    print("Success")
else:
    print("Failed")

Output
Python 3.13.0
Success

Explanation:

  • subprocess.call() executes the command
  • Returns 0 if successful
  • Output is printed directly to terminal

subprocess Pipes

Pipes are used to send input to a process and receive output from it. This helps in communication between processes.

Example: Here, input is sent to a Python process and the result is captured.

Python
import subprocess
process = subprocess.Popen(
    ["python"],
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
    text=True
)

output, _ = process.communicate("print(5 + 5)")
print(output)

Output
10

Explanation:

  • stdin=subprocess.PIPE allows sending input
  • stdout=subprocess.PIPE captures output
  • process.communicate() sends input and reads result
Comment

Explore

    Python Fundamentals
    • Introduction1 min read
    • Input & Output2 min read
    • Variables4 min read
    • Operators4 min read
    • Keywords2 min read
    • Data Types4 min read
    • Conditional Statements3 min read
    • Loops3 min read
    • Functions4 min read
    Python Data Structures
    • String4 min read
    • List3 min read
    • Tuples3 min read
    • Dictionary3 min read
    • Set2 min read
    • Arrays5 min read
    Advanced Python
    • OOP Concepts3 min read
    • Exception Handling5 min read
    • File Handling4 min read
    • Python Database2 min read
    • MongoDB3 min read
    • MySQL7 min read
    • Packages4 min read
    • Modules3 min read
    • DSA Libraries5 min read
    • Python GUI2 min read
    Data Science with Python
    • Numpy3 min read
    • Pandas4 min read
    • Matplotlib3 min read
    • Seaborn3 min read
    • StatsModel2 min read
    • Model Building6 min read
    • TensorFlow2 min read
    • PyTorch5 min read
    Web Development with Python
    • Flask3 min read
    • Django5 min read
    • Django ORM4 min read
    • Jinja2 Templating5 min read
    • Django Templates5 min read
    • REST API3 min read
    • Build API with DRF4 min read
    Python Practice
    • Quiz1 min read
    • Practice Problems2 min read
    • Interview Q & A15+ min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • AI Engg Course2 min read
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.