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. > Thanks in advance. > > #programme to calculate various parameters for a dc-generator. > import math > #import os > #flux is assumed to be .005Wb, and A=parallel paths = 2 for wave winding > polerpm=[] > for ConductorsPerSlot in range(1,11): > """ we consider that output voltage is 20 V DC """ > PoleRpmProduct=20000/ConductorsPerSlot > polerpm.append(PoleRpmProduct) > print '(Pole*RPM) product for various values of conductors/slot is: \n', > polerpm > for poles in range(2,18,2): > print > print '\n For number of poles='+str(poles) +' RPM values are: ' > for i in range(len(polerpm)): > rpm=polerpm[i]/poles > print rpm, > >
Another suggestion is to define a class that contains a method named "write" which takes one argument which is the text to be printed. This approach is useful when there are print statements in code that is not under your control, for example imported modules. There is a note about this here: http://docs.python.org/library/sys.html#sys.stdout Here is an example: # ================================================ import sys class Redirect(object): def __init__(self, filename): self.outfile = open(filename, 'w') self.count = 0 def write(self, msg): self.count += 1 self.outfile.write('%d %s\n' % (self.count, msg, )) def close(self): self.outfile.close() def test(): print 'starting' save_stdout = sys.stdout redir = Redirect('/tmp/tmp1.txt') sys.stdout = redir print 'something' print 'something else' redir.close() sys.stdout = save_stdout print 'finished' test() # ================================================ A few notes: - Be sure to close or flush the file. - The chunks passed to your write method may not be whole lines. - Dave > > -- > --prasad mehendale > > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- Dave Kuhlman http://www.rexx.com/~dkuhlman _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor