I'm trying to search a text file and retrieve lines containing a specific set of words. This is the code I'm using:
tyrs = subprocess.check_output('grep "^A" %s | grep TYR' % pocket_location, shell = True).split('\n')
This works fine when the file contains at least one line that grep identifies. But when grep doesn't identify any lines, grep returns exit status 1 and I get the following error:
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
How can I avoid this issue? I just want subprocess.check_output
to return an empty string if grep doesn't find anything.
Thanks
pipes.quote(pocket_location)
to allow the path with characters that are special in a shell such as space character. – Sherasherar