Extended Print Statement

New syntax for printing to file

     f = open("foo","w")
     print >>f, "Hello World"
     for i in range(10):
         print >>f, "i = ",i
     f.close()

Previously, print only printed to sys.stdout

     oldstdout = sys.stdout
     sys.stdout = open("foo","w")
     print "Hello World"
     for i in range(10):
         print "i = ",i
     sys.stdout.close()
     sys.stdout = oldstdout

Any file-like object can be used

  • Built-in files, StringIO, etc.
<<< O'Reilly OSCON 2001, New Features in Python 2, Slide 32
July 26, 2001, beazley@cs.uchicago.edu
>>>