1

Python 2.4.x here.

Been banging my head trying to get subprocess to work with glob.

Well, here's the problem area.

def runCommands(thecust, thedevice): thepath='/smithy/%s/%s' % (thecust,thedevice) thefiles=glob.glob(thepath + '/*.smithy.xml') p1=subprocess.Popen(["grep", "<record>"] + thefiles, stdout=subprocess.PIPE) p2=subprocess.Popen(['wc -l'], stdin=p1.stdout, stdout=subprocess.PIPE) p1.stdout.close() thecount=p2.communicate()[0] p1.wait() 

I receive numerous "grep: writing output: Broken pipe" errors on the screen.

It's got to be something simple I'm missing, I just can't spot it. Any idea?

Thank you in advance.

2
  • There are a couple (very nice) wrappers around subprocess tha will make your life a lot easier like pbs and plumbum. Commented May 31, 2012 at 18:35
  • those look really cool - unfortunately i'm not in an environment where I can add modules outside of 2.4's modules Commented May 31, 2012 at 18:43

2 Answers 2

5

The issue here is that for p2 your argument list should be ['wc', '-l'] instead of ['wc -l'].

Currently it is looking for an executable named 'wc -l' to run and not finding it, so p2 immediately fails and there is nothing connected to p1.stdout, which results in the broken pipe errors.

Try the following code:

def runCommands(thecust, thedevice): thepath='/smithy/%s/%s' % (thecust,thedevice) thefiles=glob.glob(thepath + '/*.smithy.xml') p1=subprocess.Popen(["grep", "<record>"] + thefiles, stdout=subprocess.PIPE) p2=subprocess.Popen(['wc', '-l'], stdin=p1.stdout, stdout=subprocess.PIPE) p1.stdout.close() thecount=p2.communicate()[0] p1.wait() 
Sign up to request clarification or add additional context in comments.

Comments

0

This seems to be because you are closing p1.stdout before grep is done outputting. Maybe you meant to close pt.stdin? There doesn't seem to be any reason to close either of those though, so I would just remove the p1.stdout.close() statement.

1 Comment

Take a look at the docs: docs.python.org/library/… -- It appears to be done correctly to me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.