Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python script to test an execution of external commands by subprocess.
#! /usr/local/bin/python3.6
"""
Test of subprocess module
"""
import subprocess as sp
import sys
import traceback
class TestSubprocess:
def exec(self):
cmds = ["ls", "-l"]
try:
# Capturing:args, returncode
res = sp.run(cmds)
print(res.args)
print(res.returncode)
print("---")
# Capturing:args, returncode, stdout(coding: utf-8)
res = sp.run(cmds, stdout=sp.PIPE, encoding="utf-8")
print(res.args)
print(res.returncode)
print(res.stdout)
except Exception as e:
raise
if __name__ == '__main__':
try:
obj = TestSubprocess()
obj.exec()
except Exception as e:
traceback.print_exc()
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.