SoFunction
Updated on 2025-03-01

Six ways to execute shell commands in Python

1. ()

()It is the easiest way to execute shell commands. It accepts a string as a command and executes it in the shell. The return value is the exit status code of the command. Usually 0 means success, and non-0 means failure.

import os
result = ('ls -l')
print("Exit status:", result)

Features:

  • Simple and easy to use.
  • Unable to get the output or error message of the command.
  • The timeout of the command cannot be handled.

2. ()

()is the recommended way because it provides more flexibility and functionality. It can execute commands and can capture output and error messages.

import subprocess
result = (['ls', '-l'], capture_output=True, text=True)
print("Output:", )

Features:

  • Output and errors can be captured.
  • The timeout can be set.
  • You can check the exit status of the command.

3. ()

()Provides finer granular control, allowing commands to be executed and interacted with. It returns aPopenObjects, which can be used for further operations.

import subprocess
process = (['ls', '-l'], stdout=, stderr=, text=True)
stdout, stderr = ()
print("Output:", stdout)
if  != 0:
    print("Error:", stderr)

Features:

  • The output and errors can be obtained in real time.
  • It can control the input, output and error flow of the process.
  • You can wait or poll the process for completion.

4. ()

()Used to execute commands and read output. It returns a file object that can be read like a normal file.

with ('ls -l') as stream:
    print(())

Features:

  • Simple and easy to use, suitable for scenes where only output is read.
  • Error message cannot be captured.

5. subprocess.check_output()

subprocess.check_output()Used to execute commands and get output. If the command returns a non-zero exit status code, an exception will be thrown.

from subprocess import check_output
output = check_output(['ls', '-l'], text=True)
print("Output:", output)

Features:

  • Get the command output directly.
  • If the command fails, an exception will be thrown to facilitate error handling.

6. ()

()Execute a command and wait for it to complete, returning the command's exit status code.

import subprocess
status = (['ls', '-l'])
print("Exit status:", status)

Features:

  • Simple and easy to use, only returns the exit status code.
  • The output or error message cannot be captured.

Recommendations for use

When choosing a method to execute shell commands, you should decide which method to use according to specific needs. If you need to capture output and error messages, it is recommended to use () or (). If you simply execute the command and get the exit status, () or () may be more appropriate.

In addition, when using these methods, you need to pay attention to the handling of exceptions and errors, especially when the command fails. Due to its powerful control and flexibility, the subprocess module has become the preferred way to execute shell commands.

This is the end of this article about six methods for executing shell commands in Python. For more related content on Python execution shell commands, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!