Python获取子进程的exec-code

时间:2015-01-12 23:00:55

标签: python

在Win7上我想获得EXE GUI进程的exec-code(返回代码)。它可能会返回int。使用subprocess很容易吗?

PY3。

3 个答案:

答案 0 :(得分:1)

您正在寻找的机器人是subprocess.check_call(引自API文档: https://docs.python.org/3.4/library/subprocess.html 为方便起见,我在下面引用了......

subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False)

"""
Run command with arguments. Wait for command to complete. If the return code was zero then 
return, otherwise raise CalledProcessError. The CalledProcessError object will have the 
return code in the returncode attribute.

The arguments shown above are merely the most common ones, described below in Frequently 
Used Arguments (hence the slightly odd notation in the abbreviated signature). The full   
function signature is the same as that of the Popen constructor - this functions passes    
all supplied arguments directly through to that interface.
"""

答案 1 :(得分:0)

答案 2 :(得分:0)

我现在做了这样的def(fn是exe的完整路径)

import subprocess
def get_exec_code(fn, code=0):
    try:
        subprocess.check_call([fn, str(code)])
        return 0
    except subprocess.CalledProcessError as e:
        return e.returncode