new file.write method

D

David Bear

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
to file.~n~ and then procede to write the current to file. of course
it would be neat if one could also specify the maxversions as well,
something like
file.write("name", maxver=10)
would be perfect.

I've been trying to write a library to do this myself but hit a few
snags. I realized there's more to this than I originally thought. I
wanted to try to use recursion to handle this.. but the code turned
real ugly. Here's what I have so far.

#!/usr/bin/python
#
import sys, os, os.path

def makeVer(filename, count=0, max=10):
''' makes versions of filename, filename must
have a trailing . (period), the splitext function is used
without intelligence, upon success, filename is return, on any
IOexception, None is returned
'''
try:
if count == max-1:
# print 'remove lastver'
os.remove(filename)
if os.path.exists(filename):
count += 1
nn = os.path.splitext(filename)[0] + '.~%s~' % count
dummy = makeVer(nn, count)
os.rename(filename, nn)
print "renaming " + filename
else:
# no prior versions exists under this name
pass
except IOError:
return(None)
return(filename)

def callMakeVer(filename, maxVersions=10, debug=0):
''' wraps makeVer to check for first use, as well as file name
dependancies, checks proper returns of makeVer, returns proper
file object or None on error
'''
try:
if os.path.exists(filename):
if filename[len(filename)-1] == '.':
name = filename + '.'
else:
name = filename
result = makeVer(name, 0, maxVersions)
if result == None:
return(result)
result = open(filename, 'w')
except IOError:
return(result)
return(result)



if __name__ == "__main__":
fn = sys.argv[1]
dummy = callMakeVer(fn)
if dummy == None:
print "stupid"
print type(dummy)
print dummy
 
A

Alex Martelli

David said:
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 -- safari.oreilly.com, 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).


Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top