Write both in std output and in a file ?

Philip Swartzleonard starx at pacbell.net
Wed Jan 16 04:39:33 EST 2002


Barghest || Wed 16 Jan 2002 01:54:36a:

> Hello,
> Is it possible to print output both in the console and in a file ?
> 
> Python 2.1

I don't believe there's a built in for this, but you could just do 
something like this:

def dual_print(file, string):
    	print string
    	file.write( string )
    	# You need to pass a valid file handle

There may be better ways, but this is probably better than writing the 
two statements in 50 places, which is the 'ovbious' option.

Oh, and as for the file, you'll need

    	out_file = open( "filename", "w" )

before you use the file, and optinally

    	out_file.close

after you're done (in python it's done automaticly when the file variable 
gets destroied, so it's not AS big an issue as C and such)

-- 
Philip Sw "Starweaver" [rasx] :: <nothing>



More information about the Python-list mailing list