new file.write method

Alex Martelli aleax at
Tue Nov 4 17:01:33 CET 2003

David Bear wrote:

> I'd like something like the versioning behavior in emacs for the
> file.write method.  I spent some time looking around parnassus and
> couldnt find anything there.  Google for "python file version method"
> was useless.  Looking at the lib reference, there don't seem to be any
> addition methods/attributes available for the file.write method.
> what I would like is a write method that each time is was called it
> would look for an existing file by that name, if it existed rename it

The write method of a file object takes a string of bytes and writes
it to the file object (which must already be opened for writing).  So,
it seems that it's definitely not the unbound method file.write that
you might interested in changing, but rather the constructor 'file'
(aka 'open') which is what does the open-for-writing.

If that's indeed your goal, take a look at the Python Cookbook,
recipes 4.25 and 4.26 (p. 154-159 of the printed version) -- there
will be something in the online cookbook at activestate, too, of
course, but the printed-cookbook versions tend to be cleaned up,
better discussed, etc.  If you don't own the Python Cookbook and
don't want to pay for it, I suggest you subscribe to O'Reilly's
Safari service --, I believe: the first two
weeks are free, so as long as you cancel before 14 days you will
not pay -- and two weeks is plenty to look over these, and other
neat things, in the Cookbook, the Nutshell, and other online books
(on Python and other subjects).


More information about the Python-list mailing list