tags:

views:

44

answers:

2

I have this program, which when executed on the console like this:

prog  1> output 2> error

Has the valid output and error. However, when I execute the same program using the subprocess module.

p = subprocess.Popen(['prog'],stdout=PIPE, stderr=PIPE,close_fds=True)
out, err = p.communicate()

The out is empty but err is proper. What could be happening here? I can do a os.system and direct to output and error too. But I had been relying on subprocess for doing till recently.

What could be the problem? This is being tried on Linux only. Not on Windows. `

A: 

Are you trying this on Windows?

The use of close_fds is platform dependent, according to the subprocess.Popen() doc.

If close_fds is true, all file descriptors except 0, 1 and 2 will be closed before the child process is executed. (Unix only). Or, on Windows, if close_fds is true then no handles will be inherited by the child process. Note that on Windows, you cannot set close_fds to true and also redirect the standard handles by setting stdin, stdout or stderr.

gimel
Nope, I am not trying on Windows. I am trying this on Linux.Thanks for the response.
Senthil Kumaran
Every bit of info helps...
gimel
A: 

Just tried your code and works for me:

>>> p = subprocess.Popen(['ls'], stdout=subprocess.PIPE, stderr=subprocess.PIPE,
 close_fds=True)
>>> out, err = p.communicate()
>>> out
'build\nCode\n...'
>>> err
''

a) Make sure your program is called correctly.

b) Did you import PIPE correctly?

monstru0
Yes, the program is called properly and PIPE is imported properly too.It is not the problem at those ends. Thanks anyways.
Senthil Kumaran