| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | def backupSave(fileName, prefix='', suffix='bk', count=5):
     ''' backupSave: backup a file to up to number of versions defined by count.
    
     Let fileName = 'c:/tmp/myfile.txt'
         prefix   = 'x_'
         suffix   = 'BAK'
         count    = 5 
    
     This function will check if 
    
        x_myfile.txt.BAKi
    exists in the same folder, where 1<=i<=count
    
    If found, save it to x_myfile.txt.BAKi+1
    
     version: 04b43
     module : panTools    
     author : Runsun Pan
     '''
     import os, shutil
     folder, fileName = os.path.split(fileName)
     if not folder.strip(): folder = '.'
     files = os.listdir(folder)
     
     for i in range(count):
        j = count-i-1
        fn  = prefix + fileName  + '.' + suffix + str(j)
        if j == 0: fn = fileName
        nfn = prefix + fileName +  '.' + suffix + str(j+1)
        fn = os.path.join(folder, fn)
        if os.path.exists(fn):
           shutil.copy(fn, nfn) 
 | 
backupSave('myfile.txt') ===> will copy myfile.txt to myfile.txt.bk1
run it the 2nd time ==> will copy myfile.txt.bk1 to myfile.txt.bk2 and copy myfile.txt to myfile.txt.bk1
run it the 3rd time ==> copy myfile.txt.bk2 to myfile.txt.bk3 copy myfile.txt.bk1 to myfile.txt.bk2 and copy myfile.txt to myfile.txt.bk1 ... up to a number of versions defined by the argument 'count'

 Download
Download Copy to clipboard
Copy to clipboard
my version may be cleaner ;). Hi,
with os function wrappers this task will be more pleasant, especially for smart file handling (gaps in BAK filenames) and using rename instead of copy.
And smarter... this version doesnt allow 'gaps' between backup file names, and not perform extra file renames: