我想检查子进程是否已成功执行或失败。目前我已经提出了一个解决方案,但我不确定它是否正确可靠。是否保证每个进程仅向stderr输出错误到stdout
:
注意:我对重定向/打印输出不感兴趣。我知道该怎么做。
pipe = subprocess.Popen(command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
if "" == pipe.stdout.readline():
print("Success")
self.isCommandExectutionSuccessful = True
if not "" == pipe.stderr.readline():
print("Error")
self.isCommandExectutionSuccessful = True
或者:
if "" == pipe.stdout.readline():
print("Success")
self.isCommandExectutionSuccessful = True
else:
print("Error")
self.isCommandExectutionSuccessful = False
和
if not "" == pipe.stderr.readline():
print("Success")
self.isCommandExectutionSuccessful = True
else:
print("Error")
self.isCommandExectutionSuccessful = False
答案 0 :(得分:24)
您是否需要对流程的输出做任何事情?
此处check_call
方法可能很有用。请参阅此处的python文档:https://docs.python.org/2/library/subprocess.html#subprocess.check_call
然后您可以按如下方式使用它:
try:
subprocess.check_call(command)
except subprocess.CalledProcessError:
# There was an error - command exited with non-zero code
但是,这依赖于command
返回退出代码0表示成功完成,而非零值表示错误。
如果您还需要捕获输出,那么check_output
方法可能更合适。如果您也需要,也可以重定向标准错误。
try:
proc = subprocess.check_output(command, stderr=subprocess.STDOUT)
# do something with output
except subprocess.CalledProcessError:
# There was an error - command exited with non-zero code
请参阅此处的文档:https://docs.python.org/2/library/subprocess.html#subprocess.check_output
答案 1 :(得分:3)
您可以使用check_call()方法检查流程的返回代码。 如果进程返回非零值,将引发CalledProcessError。
答案 2 :(得分:2)
检查返回码,stdout和stderr的完整解决方案:
import subprocess as sp
# ok
pipe = sp.Popen( 'ls /bin', shell=True, stdout=sp.PIPE, stderr=sp.PIPE )
# res = tuple (stdout, stderr)
res = pipe.communicate()
print("retcode =", pipe.returncode)
print("res =", res)
print("stderr =", res[1])
for line in res[0].decode(encoding='utf-8').split('\n'):
print(line)
# with error
pipe = sp.Popen( 'ls /bing', shell=True, stdout=sp.PIPE, stderr=sp.PIPE )
res = pipe.communicate()
print("retcode =", pipe.returncode)
print("res =", res)
print("stderr =", res[1])
打印:
retcode = 0
res = (b'bash\nbunzip2\nbusybox\nbzcat\n...zmore\nznew\n', b'')
stderr = b''
bash
bunzip2
busybox
bzcat
...
zmore
znew
retcode = 2
res = (b'', b"ls: cannot access '/bing': No such file or directory\n")
stderr = b"ls: cannot access '/bing': No such file or directory\n"
答案 3 :(得分:1)
output,error=pipe.communicate()
这将等待命令完成并根据命令状态给出输出或错误。
答案 4 :(得分:0)
这就是我最终做到的:
# Call a system process
try:
# universal_newlines - makes manual decoding of subprocess.stdout unnecessary
output = subprocess.check_output(command,
stderr=subprocess.STDOUT,
universal_newlines=True)
# Print out command's standard output (elegant)
for currentLine in output:
self.textEdit_CommandLineOutput.insertPlainText(currentLine)
self.isCommandExecutionSuccessful = True
except subprocess.CalledProcessError as error:
self.isCommandExecutionSuccessful = False
errorMessage = ">>> Error while executing:\n"\
+ command\
+ "\n>>> Returned with error:\n"\
+ str(error.output)
self.textEdit_CommandLineOutput.append(errorMessage)
QMessageBox.critical(None,
"ERROR",
errorMessage)
print("Error: " + errorMessage)
except FileNotFoundError as error:
errorMessage = error.strerror
QMessageBox.critical(None,
"ERROR",
errorMessage)
print("Error: ", errorMessage)
我希望它对其他人有用。