Simple question for all of you python gurus

S

Sean Berry

I am writing a little script to grep through a really long list of files and
look for a certain phrase.

I wanted to do something like this.

li = os.popen("/usr/bin/grep -i %s /home/%s/.tmda/pending" %(phrase,userid)
lis = li.readlines()
blah blah blah...

but even if I run this from the command line I get an error saying the
argument list is too long.

However, if I cd to /home/userid/.tmda/pending and then run the grep from
within the directory... it works great.

so I tried to do something like this
os.system("cd /home/%s/.tmda/pending" %userid)
li = os.popen("/usr/bin/grep -i %s" %phase)
lis = lis.readlines()

But since cd is a shell command, it does not seem to work either.

How can I do this?
Thanks in advance for answering this seemingly easy question.
 
S

Sean Berry

Thanks to you both.

os.chdir ... I knew it was something simple. Sorry for the wasted
bandwidth.
 
D

Diez B. Roggisch

How can I do this?
Thanks in advance for answering this seemingly easy question.

You're code might work if you use os.chdir to change the current working
directory.

Another that works on the command-line (and thus for your script) is to use
xargs with -l<num>, e.g.:

find / | xargs -l 10 grep -i foo

xargs then will invoke grep in batches of ten.
 
C

Cameron Laird

.
.
.
so I tried to do something like this
os.system("cd /home/%s/.tmda/pending" %userid)
li = os.popen("/usr/bin/grep -i %s" %phase)
lis = lis.readlines()

But since cd is a shell command, it does not seem to work either.

How can I do this?
.
.
.
import os

os.chdir("/home/%s/.tmda/pending" % userid)
li = os.popen(...
 
J

Jeremy Sanders

os.chdir("/home/%s/.tmda/pending" % userid)
li = os.popen(...

Wouldn't something like this also work?

li = os.popen('/bin/sh -c "cd /home/%s; /usr/bin/grep -i %s blah"')

This has the advantage that the current directory of the Python program
doesn't change.

Jeremy
 
C

Cameron Laird

Wouldn't something like this also work?

li = os.popen('/bin/sh -c "cd /home/%s; /usr/bin/grep -i %s blah"')

This has the advantage that the current directory of the Python program
doesn't change.
.
.
.
Yes.

I want to make it explicit: yes, "/bin/sh -c ..." *does* work,
it's a good technique to keep in mind, and it does have the
advantage you describe (assuming proper resolution of the %s-s
above).

Even for Python, there frequently are multiple solutions. I
speculated that the original poster would feel most comfortable
with os.chdir(), and choose not to mention other possibilities.

As valuable as "/bin/sh -c ..." is, some people avoid it for
its Unix-specificity, or even its multiplication of process
count.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top