new file.write method
david.bear at asu.edu
Tue Nov 4 01:29:25 CET 2003
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,
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.
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
if count == max-1:
# print 'remove lastver'
count += 1
nn = os.path.splitext(filename) + '.~%s~' % count
dummy = makeVer(nn, count)
print "renaming " + filename
# no prior versions exists under this name
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
if filename[len(filename)-1] == '.':
name = filename + '.'
name = filename
result = makeVer(name, 0, maxVersions)
if result == None:
result = open(filename, 'w')
if __name__ == "__main__":
fn = sys.argv
dummy = callMakeVer(fn)
if dummy == None:
More information about the Python-list