py调用cmd命令

来源:互联网 发布:出肉走淘宝 编辑:程序博客网 时间:2024/06/08 07:19

不说其他直接上代码:

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


0 1