简体   繁体   English

检查 ipython 中最后一个命令的退出状态

[英]Check the exit status of last command in ipython

Does anybody know how to check the status of the last executed command (exit code) in ipython?有人知道如何在 ipython 中检查最后执行的命令(退出代码)的状态吗?

It should be stored as _exit_code after you run the command (at least in the upcoming v0.11 release).运行命令后,它应该存储为_exit_code (至少在即将发布的 v0.11 版本中)。

I assume that you are talking about running commands from IPython using the !我假设您正在谈论使用 IPython 运行命令! escape:逃脱:

In[1]: !echo hello
hello

In[2]:

Using Google I found the documentation and there is no mention of the exit status of the command being captured anywhere.使用谷歌我找到了文档,没有提到在任何地方捕获的命令的退出状态。 Using dir() I looked for a variable name that might be holding that information and I didn't find anything.使用dir()我查找了一个可能包含该信息的变量名,但我没有找到任何东西。 I tried the x = !ls syntax and x gets set to a list of output lines from the command;我尝试了x = !ls语法并将x设置为命令中的 output 行列表; there is no exit status in there.那里没有退出状态。

In short, I don't think IPython is even capturing this information.简而言之,我认为 IPython 甚至没有捕捉到这些信息。 At this point I would want to check the source code to IPython to try to figure out anything else.在这一点上,我想检查 IPython 的源代码以尝试找出其他任何东西。

You can always just run the command with os.system() and get an exit status from that.您始终可以使用os.system()运行命令并从中获取退出状态。

In[1]: !launch_eva
launch_eva: could not open AT Field

In[2]: import os
In[3]: exit_status = os.system("launch_eva")
launch_eva: could not open AT Field

In[4]: exit_status
3

In[5]:

Thus we see the command launch_eva is returning exit status 3 when it can't open an AT Field.因此,我们看到命令launch_eva在无法打开 AT 字段时返回退出状态 3。

It seems like this is something that IPython should be saving.这似乎是 IPython 应该保存的东西。 There are plenty of little hidden variables.有很多小的隐藏变量。 You should file a feature request about this.您应该对此提出功能请求。

NOTE: This was tested in IPython 0.10.1 on Ubuntu.注意:这是在 Ubuntu 上的 IPython 0.10.1 中测试的。 Another answer, by "piotr", says that exit code will be captured in IPython 0.11, due to release soon. “piotr”的另一个答案说,退出代码将在 IPython 0.11 中捕获,即将发布。 I cloned the source code from the Git repository at https://github.com/ipython/ipython.git and tested it with python ipython.py ; I cloned the source code from the Git repository at https://github.com/ipython/ipython.git and tested it with python ipython.py ; as piotr said, the exit status is saved in a variable called _exit_status .正如 piotr 所说,退出状态保存在一个名为_exit_status的变量中。

You can store the command result in variable and you can check the variable whether it is present in locals() function or not.您可以将命令结果存储在变量中,您可以检查变量是否存在于 locals() function 中。 for example1:-例如1: -

var1="hello"
if 'var1' in locals():
print "status is false i.e 0"
else :
print "status is false i.e 1"

In this case the variable will be printed as hello, if you will not assign var1 to any value it will go to else loop.在这种情况下,变量将打印为 hello,如果您不将 var1 分配给任何值,它将 go 到 else 循环。 example2:-示例2:-

var1=subprocess.check_output('ps -aef | grep -i dmesg -wT | grep -v grep',shell=True)
if 'var1' in locals():
print "status is true i.e 0"
else :
print "status is false i.e 1"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取Python脚本中最后一个命令的退出状态 - Get exit status of last command in Python script Python检查shell命令的退出状态 - Python check exit status of a shell command IPython 脚本 - 使用状态代码退出脚本 - IPython scripting - Exit script with status code 退出 Python 脚本到 IPython 命令行进行调试 - Exit Python script to IPython command line for debugging 在 IPython 中运行 %%cython-magic 单元时,CompileError/LinkerError: "command 'gcc' failed with exit status 1" 是什么意思 - What does CompileError/LinkerError: "command 'gcc' failed with exit status 1" mean, when running %%cython-magic cell in IPython 命令“gcc”失败,退出状态为 1 - command 'gcc' failed with exit status 1 命令出错,退出状态为 1: - Command errored out with exit status 1: Python函数subprocess.check_output返回CalledProcessError:命令返回非零退出状态 - Python function subprocess.check_output returns CalledProcessError: command returns non-zero exit status python check_output失败,退出状态为1,但Popen适用于同一命令 - python check_output fails with exit status 1 but Popen works for same command subprocess.check_call:命令'['git','clone']'返回非零退出状态127 - subprocess.check_call: Command '['git', 'clone']' returned non-zero exit status 127
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM