py调用cmd命令

不说其他直接上代码:

def execFormatCmd(cmd):
    cmd = cmd.replace('\\', '/')
    cmd = re.sub('/+', '/', cmd)
#     print cmd
    ret = 0
    if platform.system() == "Windows":
        st = subprocess.STARTUPINFO
        st.dwFlags = subprocess.STARTF_USESHOWWINDOW
        st.wShowWindow = subprocess.SW_HIDE
    s = subprocess.Popen(cmd, shell=True)
    ret = s.wait()
    if ret:
        s = subprocess.Popen(cmd, stdout=subprocess.PIPE , stderr=subprocess.PIPE, shell=True)
        stdoutput, erroutput = s.communicate()
        
        log_utils.error("*******ERROR*******")
        log_utils.error(stdoutput)
        log_utils.error(erroutput)
        log_utils.error("*******************")
        cmd = 'error::' + cmd + '  !!!exec Fail!!!  '
    else:

        s = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
        stdoutput, erroutput = s.communicate()
        cmd += ' !!!exec success!!! '
        log_utils.warning(cmd)
    
    return ret


你可能感兴趣的:(py调用cmd命令)