473,397 Members | 2,099 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,397 software developers and data experts.

Help saving output onto a text file

I have been learning python during the past weeks and I have been able
to program some interesting things. Python is my first and only
programing language from which I wish to enter the programing world.

My question was: if I have a simple program that for example calculates
the squares of 2 to 100 times, how can I write the resulting output
onto a separate text file?

I know about the open() function and I can write strings and such in
text files but I'm not able to write the output of my programs.

- Thanks in advance

Jan 29 '06 #1
2 10526
co*******@gmail.com wrote:
If I have a simple program that for example calculates
the squares of 2 to 100 times, how can I write the resulting output
onto a separate text file?

I know about the open() function and I can write strings and such in
text files but I'm not able to write the output of my programs.


Simplest way:

results = open('myname.txt', 'w')
print >>results, 'This goes out.'
print >>results, 'Similarly, this is the next line.'
results.close()

Slightly more obscure, but if your program is already printing:

import sys
former, sys.stdout = sys.stdout, open('myname.txt', 'w')
<do anything that prints here>
results, sys.stdout = sys.stdout, former
results.close()

Really the closes (and swap back in the change-stdio case) should be done
in a "finally clause of a try: ... finally: ... block, but that may
not be where you are now.

So I'd really use (for myself):

Explicit file prints:
results = open('myname.txt', 'w')
try:
print >>results, 'This goes out.'
print >>results, 'Similarly, this is the next line.'
# You can call functions to print, but they need to get
# results and use the same form as above.
# If you write your code using print >>var, ...
# you can set var to None to go to the "normal" output.
finally:
results.close()

Captured "standard" output:
import sys
former, sys.stdout = sys.stdout, open('myname.txt', 'w')
try:
print 'This goes out.'
print 'Similarly, this is the next line.'
# Even if you call a function that prints here,
# its output is captured to your file
finally:
results, sys.stdout = sys.stdout, former
results.close()

--Scott David Daniels
sc***********@acm.org
Jan 30 '06 #2
I tried using the sys.stdout method and it worked. It seems pretty
simple but I dont understand the simple method you posted. Thanks for
the help, I'll mess around with it and keep on learning.

Jan 30 '06 #3

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

Similar topics

7
by: Catherine Jones | last post by:
Hi all we are getting active x component can not create object (Error No. 16) while creating an instance of File System Object in ASP client side script. Could you plz help us in solving this...
2
by: steve | last post by:
Hello, I am trying to import an image file into a form. This would be a persons picture saved in the same directory for every unique record. I don't have any problems making an action button to...
3
by: Jorge Cecílio | last post by:
Hi! I would like to export some MS-Access reports output to pdf. However, the only possibility offered by Access (afaik) for me to export formatted output is snp (snapshot) (I use MS-Office...
5
by: Jason | last post by:
Hello all, I am new to this newsgroup but look forward to participating quite a bit. I have a simple question which hopefully someone will be able to answer. I am wanting to write an app with vb...
5
by: Steve Marshall | last post by:
Hi all, I am converting an app which used a picturebox to draw graphs etc onto, then saved them to a file. I can certainly draw things onto a picturbox in VB.NET, but how do I save them to a...
1
by: Michael D. Reed | last post by:
I am using the help class to display a simple help file. I generated the help file using Word and saving it as a single page Web page (.mht extension). I show the help file with the following...
2
by: Velhari | last post by:
Hi all, I developed the following codes to implement the concept of Multiple file Uploading Process. Actually my goal in this implementation is to uploads the files onto the server and i have...
5
by: dav3 | last post by:
I am by no means an ultra slick programmer and my problem solving skills.. well they leave much to be desired. That being said I have been working on the following problem for the past few days and...
12
by: adamurbas | last post by:
ya so im pretty much a newb to this whole python thing... its pretty cool but i just started today and im already having trouble. i started to use a tutorial that i found somewhere and i followed...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.