Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - subprocess with Quotes and Pipe Grep

im having an issue trying to get a simple grep command into python. I want to take the output of the following command in a file or a list.

grep -c 'some thing' /home/user/* | grep -v :0

This is what I have, but its not working at all...

thing = str(subprocess.Popen(['grep', '-c', 'some thing', '/home/user/*', '|', 'grep', '-v', ':0'], stdout=subprocess.PIPE)

Basically I need to search files in a directory and return a result if my string is missing from any of the files in the directory.

Working Code (Thanks!!):

thing = subprocess.Popen(('grep -c "some thing" /home/user/* | grep -v ":0"' ),shell=True, stdout=subprocess.PIPE)
like image 896
Aladine Avatar asked Jan 01 '26 21:01

Aladine


1 Answers

The pipe | is a shell feature. You have to use Popen with shell=True to use it.

like image 95
Aran-Fey Avatar answered Jan 03 '26 09:01

Aran-Fey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!