Created
February 24, 2018 02:45
-
-
Save komasaru/10b4efc661e826108c83ed13c8003703 to your computer and use it in GitHub Desktop.
Python script to test an execution of external commands by subprocess.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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