Skip to the content.

Run command: subprocess

Recipes

import subprocess


try:
    p = subprocess.run(['ls', '-l'],
                       check=True,
                       timeout=1.0,
                       capture_output=True,
                       text=True,
                       encoding='utf-8',
                       errors='strict')
except subproess.TimeoutExpired:
    logging.error('timeout')
except CalledProcessError:
    logging.error('run failed')
isinstance(p.stdout, str)

References