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

时间:2014-01-07 22:41:08

标签: python grep subprocess

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

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

当文件包含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

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

由于

2 个答案:

答案 0 :(得分:9)

  

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

嗯,太糟糕了。 grep认为没有匹配失败,check_outputcheck的重点是检查失败,所以你明确要求以这种方式做事。以下是相关文档:

  

如果返回代码非零,则会引发CalledProcessError。 CalledProcessError对象将在returncode属性中包含返回代码,并在输出属性中包含任何输出。

对于grep

The following exit values shall be returned:
  0 One or more lines were selected.
  1 No lines were selected.
  >1 An error occurred.

因此,如果您希望将“无行”视为成功,将实际错误视为错误,则必须以不同于其他非零值的方式处理该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 = []

......或者这个:

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

答案 1 :(得分:5)

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