Skip to content

Instantly share code, notes, and snippets.

@komasaru
Created February 24, 2018 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komasaru/10b4efc661e826108c83ed13c8003703 to your computer and use it in GitHub Desktop.
Save komasaru/10b4efc661e826108c83ed13c8003703 to your computer and use it in GitHub Desktop.
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