subprocess.check_output return code

前端 未结 5 1846
刺人心
刺人心 2020-12-02 22:03

I am using:

grepOut = subprocess.check_output(\"grep \" + search + \" tmp\", shell=True)

To run a terminal command, I know that I can use a

5条回答
  •  无人及你
    2020-12-02 22:47

    You can get the error code and results from the exception that is raised.

    This can be done through the fields returncode and output.

    For example:

    import subprocess
    
    try:
       grepOut = subprocess.check_output("grep " + "test" + " tmp", shell=True)                       
    except subprocess.CalledProcessError as grepexc:                                                                                                   
        print "error code", grepexc.returncode, grepexc.output
    

提交回复
热议问题