当grep找不到匹配项时,带有grep命令的subprocess.check_output失败

use*_*031 6 python grep subprocess

我正在尝试搜索文本文件并检索包含一组特定单词的行.这是我正在使用的代码:

tyrs = subprocess.check_output('grep "^A" %s | grep TYR' % pocket_location, shell = True).split('\n')
Run Code Online (Sandbox Code Playgroud)

当文件包含grep标识的至少一行时,这可以正常工作.但是当grep没有标识任何行时,grep返回退出状态1并且我收到以下错误:

Traceback (most recent call last):
  File "../../Python_scripts/cbs_wrapper2.py", line 324, in <module>
    tyrs = subprocess.check_output('grep "^ATOM" %s | grep TYR' % pocket_location, shell = True).split('\n')
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 544, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command 'grep "^ATOM" cbsPrediction_files/1u9c_clean/1u9c_clean_fpocket_out/pockets/pocket0_atm.pdb | grep TYR' returned non-zero exit status 1
Run Code Online (Sandbox Code Playgroud)

我该如何避免这个问题?subprocess.check_output如果grep找不到任何内容,我只想返回一个空字符串.

谢谢

aba*_*ert 9

我只是希望subprocess.check_output在grep找不到任何内容时返回一个空字符串.

好吧,太糟糕了.grep认为没有匹配是失败的,并且checkin的重点check_output是检查失败,所以你明确要求以这种方式做事.以下是相关文档:

如果返回码非零,则会引发CalledProcessError.CalledProcessError对象将具有returncode属性中的返回代码以及output属性中的任何输出.

并为grep:

The following exit values shall be returned:
  0 One or more lines were selected.
  1 No lines were selected.
  >1 An error occurred.
Run Code Online (Sandbox Code Playgroud)

因此,如果您希望将"无行"视为成功,将实际错误视为错误,则必须以1不同于其他非零值的方式处理该值.并且check_output不知道你想要那样做.

所以,要么你必须处理CalledProcessError,要么你必须自己检查.换句话说,要么:

try:
    tyrs = subprocess.check_output('grep "^A" %s | grep TYR' % pocket_location, shell = True).split('\n')
except subprocess.CalledProcessError as e:
    if e.returncode > 1:
        raise
    tyrs = []
Run Code Online (Sandbox Code Playgroud)

… 或这个:

p = subprocess.Popen('grep "^A" %s | grep TYR' % pocket_location, shell=True,
                     stdout=subprocess.PIPE)
output, _ = p.communicate()
if p.returncode == 1: # no matches found 
    tyrs = []
elif p.returncode == 0: # matches found
    tyrs = output.split('\n')
else:
    # error, do something with it
Run Code Online (Sandbox Code Playgroud)


yac*_*ccz 5

tyrs = subprocess.check_output('grep "^A" %s | grep TYR || true' % pocket_location, shell = True).split('\n')
Run Code Online (Sandbox Code Playgroud)

  • shell命令以call为true结束,确保返回码始终为0 (3认同)