473,385 Members | 1,267 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

execute python code and save the stdout as a string

Hello,

I've a question. Can I execute a part of a python code and put it's
output in a string? Something like this:

s = ""
s = exec """print "Hello World" """
print s

Greetz,

Noud

Jul 19 '05 #1
2 1753
jwaixs wrote:
I've a question. Can I execute a part of a python code and put it's
output in a string?

import sys
from cStringIO import StringIO

def exec_and_get_output(code): ... old_stdout = sys.stdout
... sys.stdout = StringIO()
... try:
... exec code in {}, {}
... return sys.stdout.getvalue()
... finally:
... sys.stdout = old_stdout
... exec_and_get_output("print 'foo'")

'foo\n'
Jul 19 '05 #2
Thank you, this really looks cool!

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: sarmin kho | last post by:
Hi Pythoners.. "print (text)' command will print the 'text' on dos window when executing a python script. i would like to save all the text printed on the dos window to a file at the end of the...
6
by: Farshid Lashkari | last post by:
Hi, My application has python embedded into it. I noticed that when I run any python code the output is buffered and doesn't get flushed until my application exits. To fix this I simply flush...
10
by: A.M | last post by:
Hi, I am having difficulty with shell scripting in Python. I use the following command to run a DOS command and put the return value in a Python variable:
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 349 open ( +7) / 3737 closed (+25) / 4086 total (+32) Bugs : 939 open (-12) / 6648 closed (+60) / 7587 total (+48) RFE : 249 open...
1
by: Colly | last post by:
How do I output the IDLE 1.2.1 Python Shell results to a log file.
3
by: koutoo | last post by:
Is it possible to display messages in the python shell? I want to display error messages based on parameters in my scripts to the users. Is there another way to display messages other than log...
1
by: raocheng | last post by:
Please see the following code. Suppose I have many shell commands to be executed. And I don't want to fork a sub shell for each command(eg: status,output = commands.getstatusoutput(cmd)) because...
1
by: Hishaam | last post by:
Hi, In python documentation, i found a telnet example as follows: ------------------------------------------------------------------------- import getpass import sys import telnetlib HOST...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.