Skip to content Skip to sidebar Skip to footer

Subprocess.popen Behaves Differently In Interpreter, Executable Scripts

Let's say you have the following: command = shlex.split('mcf -o -q -e -w %s %s' % (SOLFILE, NETFILE)) task = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIP

Solution 1:

I wrote a little test script to test the subprocess module with.

#!/bin/bashechoecho to stderr 1>&2
echoecho to stdout

Then I wrote a small Python script that calls it:

#!/usr/bin/python

import subprocess

command = ('./joe.sh',)
task = subprocess.Popen(command, stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
stdout, stderr = task.communicate()
print'stdout == %r\nstderr == %r' % (stdout, stderr)

The output of running it looks just like this:

$ python joe.py 
stdout == 'echo to stdout\n'stderr == 'echo to stderr\n'

The output of running that same sequence in ipython is the same.

So the subprocess module is behaving in the manner you expect, and not how it's behaving for you in your question. I think something other than the subprocess module must be at fault here because what you're doing works for me.

I'm running Python 2.7, so another possibility is that maybe there is some kind of weird bug in older versions of the subprocess module.

Post a Comment for "Subprocess.popen Behaves Differently In Interpreter, Executable Scripts"