Python Module subprocess

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

External

Internal

Overview

Execute an O/S Command

import subprocess
helm = subprocess.Popen(['helm', 'package', './my-chart'],
                        cwd = str(Path(project_dir, 'build/helm')),
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE,
                        universal_newlines=True)
stdout, stderr = helm.communicate()
print(stdout)
print(stderr)

Need standard failure handling patter.

TO PROCESS: https://janakiev.com/blog/python-shell-commands/

subprocess.run()

import subprocess

completed_process = subprocess.run(
    'ls',
    capture_output=True,
    check=True,
    shell=True,
)

print(completed_process.stdout)