[Tutor] creating files with python and a thanks
Sun, 17 Feb 2002 10:48:50 +0100
> We can also 'print' to a file, like this:
> >>> f = open('output.txt', 'w')
> >>> print >>f, "The square of 42 is", square(42)
> >>> f.close()
> >>> print open('output.txt').read()
> The square of 42 is 1764
> In this case, we can use the 'print >>file' statement, and since you're
> already familiar with how 'print' works, this should be a good way of
> doing file output.
Arh, no! Please don't let this monster escape to the daylight!
> (On the other hand, I can't say that I _like_ the syntax of this extended
> 'print' statement; I would have much preferred something like:
> fprint f, "The square of 42 is", square(42)
> because at least it avoids a semantic conflict with the shift-right
> operator, and seems to fix my internal model of Python. Has anyone written
> a PEP request to change the syntax of 'print >>file' yet?)
I think such a PEP would be doomed, considering that there was a lot
of syntax discussion when the >> PEP was written and that incompatible
changes are frowned upon if they lack a technical motivation.
> Ignore my grumbling.
Kalle Svensson (email@example.com) - Laziness, impatience, hubris: Pick two!
English: http://www.gnupung.net/ Svenska: http://www.lysator.liu.se/~kalle/
Stuff: ["http://www.%s.org/" % x for x in "gnu debian python emacs".split()]