Using print instead of file.write(str)

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Thu Jun 1 19:45:36 EDT 2006


A.M a écrit :
> Hi,
> 
> 
> I found print much more flexible that write method. Can I use print instead 
> of file.write method?
> 

f = open("/path/to/file")
print >> f, "this is my %s message" % "first"
f.close()

To print to stderr:

import sys
print >> sys.stderr, "oops"

FWIW, you and use string formating anywhere, not only in print statements:

s = "some %s and % formating" % ("nice", "cool")
print s

You can also use "dict formating":

names = {"other": "A.M.", "me" : "bruno"}
s = "hello %(other)s, my name is %(me)s" % names



More information about the Python-list mailing list