我正在嘗試搜索文本文件並檢索包含特定字詞集的行。這是我正在使用的代碼:當grep找不到匹配項時,使用grep命令執行subprocess.check_output失敗
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
返回一個空字符串。
感謝
使用'pipes.quote(pocket_location)'允許與處於一個特殊的外殼,如空格字符的路徑。 – jfs