subprocess.Popen with grep reading multiple files

This is the place for queries that don't fit in any of the other categories.

subprocess.Popen with grep reading multiple files

Postby eole-so » Sun Jun 15, 2014 4:41 pm

Hello all,
New to this forum and python. Mother tongue is French, so don't be harsh on my English and spelling mistakes.
Trying to use grep thru a gui to search a big library of books in html. I use string variables for "pattern" and absolute path to file or folder.
While I've no problem searching one file, I get stuck when trying to search multiple files. The code below explains what works and what don't:
Code: Select all
#!/usr/bin/env python
# -*- coding: latin-1 -*-
# subproc.py

import subprocess

# two lines below work ok
# cmd = """grep -ie "d'estienne d'orves" /media/lydia/Textes/textes/barber/ch1.htm"""
# process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

# two lines below also work ok
# cmd = """grep -ie "d'estienne d'orves" /media/lydia/Textes/textes/barber/*"""
# process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

# following searching one file, works fine
# pattern = "d'estienne d'orves"
# fileStr = "/media/lydia/Textes/textes/barber/ch1.htm"
# process = subprocess.Popen(["grep", "-ie", pattern, fileStr], stdout=subprocess.PIPE)

# following searching any file, gives error: No such file or directory
pattern = "d'estienne d'orves"
fileStr = "/media/lydia/Textes/textes/barber/*"
process = subprocess.Popen(["grep", "-ie", pattern, fileStr], stdout=subprocess.PIPE)

# wait for the process to terminate
for line in process.stdout:
   print (line)

I would appreciate your help very much since my program is'nt worth a dime without the capability to search multiple files...
eole-so
 
Posts: 2
Joined: Fri Jun 13, 2014 3:08 pm

Re: subprocess.Popen with grep reading multiple files

Postby stranac » Mon Jun 16, 2014 9:51 am

If you're only running grep, why use python?
If you're gonna do it in python, why use grep?

Expanding * is a shell thing, not a python thing so naturally it won't work in python.
(Using shell=True would probably work, but it's not recommended)

If you want to do this using python, take a look at glob and re (if you have more complicated patterns).
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: subprocess.Popen with grep reading multiple files

Postby eole-so » Mon Jun 16, 2014 12:27 pm

@ stranac,
Thanks a lot for your prompt and enlightning reply.
For my own needs, bash command and grep are satisfactory. But I wanted to allow others with no bash script knowledge to use the grep command, therefore my pygtk program with buttons to click and gtk.Entry() to type pattern.
I will go on with python using glob (which i ignored its existence until now), fnmatch, os.listdir() etc.
eole-so
 
Posts: 2
Joined: Fri Jun 13, 2014 3:08 pm


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], snippsat and 6 guests