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) 1 8262
En Mon, 27 Oct 2008 16:03:45 -0200, TP <Tr**********@paralleles.invalid>
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Marko Faldix |
last post by:
Hello,
with Python 2.3 I can write umlauts (a,o,u umlaut) to a file with this piece
of code:
import codecs
f = codecs.open("klotentest.txt", "w", "latin-1")
print >>f, unicode("My umlauts...
|
by: jas |
last post by:
I would like to redirect the output from os.system to a variable, but
am having trouble. I tried using os.popen(..).read() ...but that
doesn't give me exactly what i want.
...this is windows by...
|
by: Edwin G. Castro |
last post by:
I want to start a process from a C# application. I also want to
redirect standard error to standard output so that I can read output
from both streams just like I could from a command line. In...
|
by: Nadav |
last post by:
Hi,
Introduction:
***************
I am trying to redirect stdout to a RichEdit control, this is done by
initiating a StringWriter, associated it with a StringBuilder and setting the...
|
by: souissipro |
last post by:
Hi,
I have written a C program that does some of the functionalities
mentionned in my previous topic posted some days ago. This shell
should:
1- execute input commands from standard input,...
|
by: prajil |
last post by:
Hi,
Could anyone point how can i override output redirection using perl.
i.e.
command > file 2>&1 will redirect both output and error of command to file
I need to print a message to...
|
by: Phil |
last post by:
I have a php script that queries some Oracle DB and outputs a single
line of plain text with <brat the end for each query. This is
Apache2, php4.4.8 and Oracle Instant Client 10.1.0.5 all on CentOS...
|
by: sab |
last post by:
Hello,
I have been working on a python script to parse a continuously growing
log file on a UNIX server. The input is the standard in, piped in
from the log file. The application works well...
|
by: fx5900 |
last post by:
Hi,
i am trying to convert an .osm (openstreetmap) file into gml format and
finally to shapefile given this wiki info
http://wiki.openstreetmap.org/index.php/GML. I'm using windows and when i...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
header("Location:".$urlback);
Is this the right layout the...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
| |