[BangPypers] Redirection of standard output to a variable

Anand Balachandran Pillai abpillai at gmail.com
Mon May 12 09:04:29 CEST 2008


Using os.popen for this is straight-forward.

Example...

[anand at localhost programming]$ python
Python 2.4.4 (#1, Oct 23 2006, 13:58:00)
[GCC 4.1.1 20061011 (Red Hat 4.1.1-30)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> f = os.popen("ls")
>>> print f.read()
#809.ods
berkleyDB.pdf
bittorrentecon.pdf
btorrent_protocol_files
btorrent_protocol.html
c_network_prog_presentation.pdf
...

You simply open a pipe to the process and read the output from
the pipe.

os.popen2/os.popen3 gives more control, using this you can manipulate both
stdio and stdout and stdio, stdout and stderr of the process respectively.

--Anand


On Mon, May 12, 2008 at 12:18 PM, S.Ramaswamy <srsy70 at gmail.com> wrote:
>>
>> I am trying to find a way from which I could redirect standard output to a
>> variable.
>
> You can redirect stdout and stderr to a file. Check out the following
> section from Dive into Python, that has sample code:
>
> http://www.diveintopython.org/scripts_and_streams/stdin_stdout_stderr.html
>
> Ramaswamy
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
-Anand


More information about the BangPypers mailing list