By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,827 Members | 2,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,827 IT Pros & Developers. It's quick & easy.

How do I automatically redirect stdout and stderr when using os.popen2?

P: n/a
How do I automatically redirect stdout and stderr when using os.popen2
to start a long running process. If the process prints a lot of stuff
to stdout it will eventually stop because it runs out of buffer space.
Once I start reading the stdout file returned by os.popen2 then the
process resumes. I know that I could just specify > /dev/null when
starting the process but I'd like to know if there is a way to start a
process using os.popen2 or some other way so that all standard out and
standard error goes to /dev/null or some other file.

Thanks,
Mike

Jun 7 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
mi*****@gmail.com wrote:
How do I automatically redirect stdout and stderr when using os.popen2
to start a long running process. If the process prints a lot of stuff
to stdout it will eventually stop because it runs out of buffer space.
Once I start reading the stdout file returned by os.popen2 then the
process resumes. I know that I could just specify > /dev/null when
starting the process but I'd like to know if there is a way to start a
process using os.popen2 or some other way so that all standard out and
standard error goes to /dev/null or some other file.

Thanks,
Mike

Use the POpen class <http://docs.python.org/lib/node235.html> from the
subprocess module.
Bye,
Dennis
Jun 7 '06 #2

P: n/a
On 2006-06-07, mi*****@gmail.com <mi*****@gmail.com> wrote:
How do I automatically redirect stdout and stderr when using os.popen2
to start a long running process.
popen2 does redirect stdout to a file object. That's the whole
point of using it. If you don't want a file object that's
connected to the child's stdout, then don't use popen2.
If the process prints a lot of stuff to stdout it will
eventually stop because it runs out of buffer space. Once I
start reading the stdout file returned by os.popen2 then the
process resumes. I know that I could just specify > /dev/null
when starting the process but I'd like to know if there is a
way to start a process using os.popen2 or some other way so
that all standard out and standard error goes to /dev/null or
some other file.


Yes. Fork a child process then use the standard file
descriptor operators to open /dev/null or some other file and
then dup those file descriptors to stdout/stderr:

http://docs.python.org/lib/os-fd-ops.html

Then you can exec the program you want to run:

http://docs.python.org/lib/os-process.html

This is basically identical to the method use to do I/O
redirection in C, so any decent book on C programming under
Unix should have a good explanation.
Hmmm, it looks like it's simpler touse the subprocess module.
Just open /dev/null to get a file descriptor for it, and then
pass that to subprocess.Popen():

http://docs.python.org/lib/module-subprocess.html

--
Grant Edwards grante Yow! I selected E5... but
at I didn't hear "Sam the Sham
visi.com and the Pharoahs"!
Jun 7 '06 #3

P: n/a
Here is what I ended up doing:

si = file('/dev/null', 'r')
so = file('/dev/null', 'a+')
i,o = os.popen2('some_command_that_prints_a_lot_to_stdou t')
os.dup2(so.fileno(), o.fileno())
os.dup2(si.fileno(), i.fileno())

Thanks for your help,
Mike

Grant Edwards wrote:
On 2006-06-07, mi*****@gmail.com <mi*****@gmail.com> wrote:
How do I automatically redirect stdout and stderr when using os.popen2
to start a long running process.


popen2 does redirect stdout to a file object. That's the whole
point of using it. If you don't want a file object that's
connected to the child's stdout, then don't use popen2.
If the process prints a lot of stuff to stdout it will
eventually stop because it runs out of buffer space. Once I
start reading the stdout file returned by os.popen2 then the
process resumes. I know that I could just specify > /dev/null
when starting the process but I'd like to know if there is a
way to start a process using os.popen2 or some other way so
that all standard out and standard error goes to /dev/null or
some other file.


Yes. Fork a child process then use the standard file
descriptor operators to open /dev/null or some other file and
then dup those file descriptors to stdout/stderr:

http://docs.python.org/lib/os-fd-ops.html

Then you can exec the program you want to run:

http://docs.python.org/lib/os-process.html

This is basically identical to the method use to do I/O
redirection in C, so any decent book on C programming under
Unix should have a good explanation.
Hmmm, it looks like it's simpler touse the subprocess module.
Just open /dev/null to get a file descriptor for it, and then
pass that to subprocess.Popen():

http://docs.python.org/lib/module-subprocess.html

--
Grant Edwards grante Yow! I selected E5... but
at I didn't hear "Sam the Sham
visi.com and the Pharoahs"!


Jun 7 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.