Skip to content Skip to sidebar Skip to footer

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/* | gre

Solution 1:

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

Solution 2:

To emulate the shell pipeline in Python, see How do I use subprocess.Popen to connect multiple processes by pipes?:

#!/usr/bin/env python
import os
from glob import glob
from subprocess import Popen, PIPE

p1 = Popen(["grep", "-c",  'some thing'] + glob(os.path.expanduser('~/*')),
           stdout=PIPE)
p2 = Popen(["grep", "-v", ":0"], stdin=p1.stdout)
p1.stdout.close()
p2.wait()
p1.wait()

To get output as a string, set stdout=PIPE and call output = p2.communicate()[0] instead of p2.wait().

To suppress error messages such as "grep: /home/user/dir: Is a directory", you could set stderr=DEVNULL.

You could implement the pipeline in pure Python:

import os
from glob import glob

for name in glob(os.path.expanduser('~/*')):
    try:
        count = sum(1for line inopen(name, 'rb') ifb'some thing'in line)
    except IOError:
        pass# ignoreelse:
        if count: # don't print zero countsprint("%s:%d" % (name, count))

Post a Comment for "Python - Subprocess With Quotes And Pipe Grep"