Delete Set public Set private Add tags Delete tags
  Add tag   Cancel
  Delete tag   Cancel
  • • DevOps notes •
  •  
  • AI
  • Tags
  • Login

Handling Subprocess Errors/shaare/wBVnmg

  • python
  • python

Handling Subprocess Errors

  • External commands can fail in multiple ways: non-zero exit codes, missing executables, or hanging processes.
  • Using subprocess.run(..., check=True) shifts return-code checks into exceptions you can catch.
  • Specific exception types (CalledProcessError, FileNotFoundError, TimeoutExpired) let you distinguish failure modes and respond appropriately.

subprocess.CalledProcessError Attributes

  • e.returncode: the non-zero exit status of the command.
  • e.cmd: the exact command invoked (list or string form).
  • e.stdout / e.output: captured standard output, if capture_output=True.
  • e.stderr: captured standard error, if capture_output=True.
  • These attributes let you log or display detailed diagnostics when a command fails.
import subprocess

cmd = ["ls", "missing_dir"]

try:
    subprocess.run(cmd, check=True, capture_output=True, text=True)
except subprocess.CalledProcessError as err:
    print(f"Command executed: {err.cmd}")
    print(f"Return code {err.returncode}")
    print(f"STDOUT capture: {err.stdout}")
    print(f"STDERR capture: {err.stderr}")

Handling FileNotFoundError

  • If the executable itself isn’t in PATH, subprocess.run() raises FileNotFoundError before running.
  • Catching it separately lets you inform the user that a required tool isn’t installed, rather than treating it as a generic failure.
import subprocess

cmd = ["fakecmd", "--version"]

try:
    subprocess.run(cmd, check=True, capture_output=True, text=True)
except FileNotFoundError as err:
    print("FileNotFoundError caught!")
    print(f"  The command '{cmd[0]}' was not found on this system.")

Handling subprocess.TimeoutExpired

  • Adding timeout=<seconds> to subprocess.run() kills the process if it runs too long.
  • A TimeoutExpired exception is raised, containing cmd, timeout, and any partial stdout/stderr.
  • Use this to prevent hung scripts and to implement retry or fallback logic.
import subprocess

cmd = ["sleep", "5"]

try:
    subprocess.run(cmd, timeout=2, capture_output=True, text=True)
    print("Command completed within timeout.")
except subprocess.TimeoutExpired as err:
    print("TimeoutExpired caught!")
    print(f"  Command: {err.cmd}")
    print(f"  Timeout after {err.timeout} seconds")

Recommended Error Handling Strategy

  • Wrap subprocess.run() in a try block.
  • First catch FileNotFoundError to detect missing executables.
  • Next catch subprocess.TimeoutExpired if you use timeouts.
  • Then catch subprocess.CalledProcessError for non-zero exits.
  • Finally, if necessary, an except Exception block can log any other unexpected issues.
  • This layered approach keeps your script robust and your errors informative.

+++

1 month ago Permalink
cluster icon
  • Filesystem Operations : Filesystem Operations (os & shutil) DevOps scripts often need to create, delete, copy, and move files and directories as part of automation workflows...
  • Log Levels in Practice : Log Levels in Practice Python defines five standard levels with increasing severity: DEBUG (10): Detailed diagnostic information. INFO (20): Confirm...
  • Adding Type Hints to Decorators and Generators : Adding Type Hints to Decorators and Generators Decorators and generators are advanced constructs that require specialized type hints to make their tr...
  • Pytest Markers : Pytest Markers Markers are decorators (@pytest.mark.) applied to tests to attach metadata. Built-in markers like skip, skipif, xfail, and parametrize...
  • Handling Authentication : Handling Authentication APIs often require authentication to control access, rate limits, and auditing. Without authentication, requests to protected...


(97)
Filter untagged links
Fold Fold all Expand Expand all Are you sure you want to delete this link? Are you sure you want to delete this tag? The personal, minimalist, super-fast, database free, bookmarking service by the Shaarli community