Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > redirection of standard output of a Python command to a Python variable

Reply
Thread Tools

redirection of standard output of a Python command to a Python variable

 
 
TP
Guest
Posts: n/a
 
      10-27-2008
Hi everybody,

I try to find a quick way to redirect the standard output of a Python
command (for example: print "message") to a python variable "foobar".
Ok, in this simple example, I could do foobar = "message", but in
fact 'print "message"' could be replaced by any Python function writing on
standard output.
I have googled on this subject.
To redirect to a variable, I could use a temporary file:

import sys
saveout = sys.stdout
fsock = open('out.log', 'w')
sys.stdout = fsock
print 'message'
sys.stdout = saveout
fsock.close()
fsock = open('out.log', 'r')
foobar = fsock.read()
fsock.close()
print "foobar= ", foobar

To redirect a system standard output directly to a variable (without
temporary file), I could do:

import os
f = os.popen("ls")
print f.read()
f.close()

But, how can I get the standard output of a Python command (print "message")
directly into a variable?

Thanks

Julien

--
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.9&1+,\'Z
(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      10-27-2008
En Mon, 27 Oct 2008 16:03:45 -0200, TP <(E-Mail Removed)>
escribió:

> Hi everybody,
>
> I try to find a quick way to redirect the standard output of a Python
> command (for example: print "message") to a python variable "foobar".
> Ok, in this simple example, I could do foobar = "message", but in
> fact 'print "message"' could be replaced by any Python function writing
> on
> standard output.
> I have googled on this subject.
> To redirect to a variable, I could use a temporary file:
>
> import sys
> saveout = sys.stdout
> fsock = open('out.log', 'w')
> sys.stdout = fsock
> print 'message'
> sys.stdout = saveout
> fsock.close()
> fsock = open('out.log', 'r')
> foobar = fsock.read()
> fsock.close()
> print "foobar= ", foobar


You are close - but instead of using a real file, look at the StringIO
module [1]

import sys
from cStringIO import StringIO

old_stdout = sys.stdout
sys.stdout = stdout = StringIO()
print 'message'
sys.stdout = old_stdout
foobar = stdout.getvalue()
print "foobar= ", foobar

(In case you start playing with this and make a mistake, you can restore
the original stdout from sys.__stdout__)

[1] http://docs.python.org/library/stringio.html

--
Gabriel Genellina

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Standard error redirection in backquote notation Vincent Lambert Ruby 0 02-25-2009 05:23 PM
How to redirect a "system" standard output and standard error to avariable (Linux) Venks Ruby 5 12-06-2007 12:21 AM
How to redirect a "system" standard output to a variable Venks Ruby 2 11-28-2007 08:03 AM
Help with python output redirection fatalserpent Python 7 04-16-2006 02:20 PM
eclipse, pydev and wxpython - standard output redirection ? Grzegorz Python 2 06-15-2005 02:43 PM



Advertisments