[Tutor] saving output data in a file

Senthil Kumaran orsenthil at gmail.com
Fri Dec 4 10:10:24 CET 2009


On Fri, Dec 04, 2009 at 01:13:42PM +0530, Prasad Mehendale wrote:
> I am a beginner. I want to save the output data of the following programme in 
> a file through the programme. Please suggest me the way. I am using Python 
> 2.3.3 on mandrake linux 10 and using "Idle" to save the output to a file 
> presently. 

To save the output to a file, you have a open a file object and write
to it. When you are done, just close it. 

If your program, open a fileobj somewhere on the top.

fileobj = open('dc-generator-output.txt','w')

And where you put the print statement, replace it with
fileobj.write("       ") # Within in the " " the output which you want
to put. One strategy would be create a string in place of your print
and write the string.
For eg.

Instead of

print '(Pole*RPM) product for various values of conductors/slot is: \n', polerpm

You will do

msg = '(Pole*RPM) product for various values of conductors/slot is: \n', polerpm

fileobj.write(msg)

And in the end, close the fileobj.

fileobj.close()

-- 
Senthil
Flee at once, all is discovered.


More information about the Tutor mailing list