简体   繁体   English

plumbum.commands.processes.ProcessExecutionError:对于返回 null 的命令

[英]plumbum.commands.processes.ProcessExecutionError: for commands which return null

The shell command I want to run, which returns nothing:我想运行的 shell 命令,它什么都不返回:

echo helloWorld | grep 'dummy'

plumbum version:铅版:

Following line works:以下线路工作:

out=(echo["helloWorld"] | grep["h"])().strip()

But following line does not, what might be the reason?但以下行没有,可能是什么原因?

out=(echo["helloWorld"] | grep["dummy"])().strip()
print(out)

Error I am having:我遇到的错误:

Traceback (most recent call last):
  File "dd.py", line 6, in <module>
    out=(echo["helloWorld"] | grep["dummy"])().strip()
  File "/home/user/venv/lib/python3.5/site-packages/plumbum/commands/base.py", line 103, in __call__
    return self.run(args, **kwargs)[1]
  File "/home/user/venv/lib/python3.5/site-packages/plumbum/commands/base.py", line 240, in run
    return p.run()
  File "/home/user/venv/lib/python3.5/site-packages/plumbum/commands/base.py", line 201, in runner
    return run_proc(p, retcode, timeout)
  File "/home/user/venv/lib/python3.5/site-packages/plumbum/commands/processes.py", line 232, in run_proc
    return _check_process(proc, retcode, timeout, stdout, stderr)
  File "/home/user/venv/lib/python3.5/site-packages/plumbum/commands/processes.py", line 23, in _check_process
    proc.verify(retcode, timeout, stdout, stderr)
  File "/home/user/venv/lib/python3.5/site-packages/plumbum/commands/base.py", line 412, in verify
    dstproc_verify(retcode, timeout, stdout, stderr)
  File "/home/user/venv/lib/python3.5/site-packages/plumbum/machines/base.py", line 26, in verify
    stderr)
plumbum.commands.processes.ProcessExecutionError: Command line: ['/bin/grep', 'dummy']
Exit code: 1

[Q] How could I fix this error? [Q]我该如何解决这个错误?

This happens because the exit status of grep is 1 if it does not find anything, as described in its manual发生这种情况是因为 grep 的退出状态是 1 如果它没有找到任何东西, 如其手册中所述

You can try it in command line if you wish:如果您愿意,可以在命令行中尝试:

echo helloWorld | grep h; echo $?
echo helloWorld | grep x; echo $?

Will result in会导致

helloWorld
0
1

Ways to circumvent this are described in another nice answer , eg避免这种情况的方法在另一个不错的答案中进行了描述,例如

echo helloWorld | grep x | cat

will yield 0 as status.将产生 0 作为状态。 But unfortunately plumbum does a local pipe mechanism so grep output goes to plumbum, and then plumbum pipes it to the next command - meaning the cat can't swallow the exit code 1, there will be an exception thrown before it.但不幸的是,plumbum 做了一个本地管道机制,所以 grep 输出到 plumbum,然后 plumbum 将它管道到下一个命令——这意味着猫不能吞下退出代码 1,在它之前会抛出一个异常。

So my two ideas are to either create a shell script to run a grep never returning error on search without results:所以我的两个想法是创建一个 shell 脚本来运行 grep 从不返回没有结果的搜索错误:

#!/bin/bash
grep "$@" || test $? = 1

and execute this instead of the grep (called c1grep in the original answer), or to add a try/except block around your pipe code and manually handle the exit code 1 (ProcessExecutionError).并执行它而不是 grep(在原始答案中称为 c1grep),或者在管道代码周围添加一个 try/except 块并手动处理退出代码 1 (ProcessExecutionError)。

The other answer is correct but less than optimal - The problem can be solved in plumbum, without the need for an external script that wraps grep , and without having to catch the exception.另一个答案是正确的,但不是最优的 - 该问题可以在 plumbum 中解决,不需要包装grep的外部脚本,也不必捕获异常。

The idea is to use plumbum's run method to to do two things:这个想法是使用 plumbum 的run方法来做两件事:

  • Force plumbum to accept any exit code from the called program (by default, an exception is raised if the returned error code is not zero).强制铅垂接受来自被调用程序的任何退出代码(默认情况下,如果返回的错误代码不为零,则会引发异常)。
  • Capture a tuple containing the error code, the standard output, and the standard error of the called program, and not just the standard output as plumbum normally does.捕获包含错误代码、标准输出和被调用程序的标准错误的元组,而不是像通常那样只包含标准输出。

Working example:工作示例:

for s in ["hello","dummy"]:
    exitcode,stdout,stderr = (echo["helloWorld"] | grep[s]).run (retcode=None)

    if exitcode == 0:
        print (stdout.strip())

    if exitcode != 0:
        print (f"string {s} not present")
        print (stderr)

The previous code returns:前面的代码返回:

helloWorld
string dummy not present

Without raising any exception.没有提出任何异常。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM