Simple question for all of you python gurus

Cameron Laird claird at
Fri Feb 27 14:01:06 CET 2004

In article <pan.2004. at>,
Jeremy Sanders  <jeremy+plusnews at> wrote:
>On Thu, 26 Feb 2004 23:14:33 +0000, Cameron Laird wrote:
>>   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.

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

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

Cameron Laird <claird at>

More information about the Python-list mailing list