我试图从批处理文件中获取退出代码。更具体地说,我在获取错误级别时遇到了问题。在
我试过使用Popen,check_output,call,check_call:
out = os.system(BatchFilePath)
out, err = subprocess.Popen(BatchFilePath,stderr=subprocess.PIPE, shell=True).communicate()
out,err = subprocess.Popen(BatchFilePath, stderr=subprocess.PIPE).communicate()
out = subprocess.Popen(BatchFilePath, shell=True).stderr
out = os.system(BatchFilePath)
out = subprocess.check_call(BatchFilePath)
out = subprocess.call(BatchFilePath, shell=True)
out = subprocess.check_output(buildPath, shell=True)
大多数情况下返回空的还是0
我也试过用
^{pr2}$但是没有运气。我也试过了
set RC=
setlocal
somecommand.exe
endlocal & set RC=%ERRORLEVEL%
exit /B %RC%
另一种方法是
out, err = subprocess.Popen(BatchFilePath,stdout=subrocess.PIPE,stderr=subprocess.PIPE, shell=True).communicate()
并从out变量中搜索字符串“ERROR”或“FAILURE”。在
另一方面,这样做用户将看不到批处理文件中的所有回声,因此屏幕将是空的,没有任何消息,直到批处理文件完成并从python脚本打印出适当的消息。在
所以我不需要使用stdout=子工艺管道选项从Popen,因为它打印所有的回声批。在
我在和CMD一起工作,而不是powershell。 我正在使用python和7
我在谷歌和这里搜索,但没有找到任何对我有帮助的东西。 任何帮助都将不胜感激。在
我的问题的解决办法其实很简单,但却很伤脑筋。在
我替换:
与
^{pr2}$我用了
胜过一切。在
谢谢@olricson,感谢您抽出时间来帮助我。我很感激。在
要使用}方法。在
subprocess.Popen
获取返回代码,请使用poll()
或{下面是一个使用
poll()
的示例:此处提供文档:https://docs.python.org/2/library/subprocess.html#subprocess.Popen.poll
根据你的评论,我用你的批处理脚本检查过了
^{pr2}$如果您将
!
替换为%
,它将起作用相关问题 更多 >
编程相关推荐