2

can some one please provide me with an explanation of the code especially the use of maxversions and statements following the line "for f in files:".

I want to understand what xrange(MAXVERSION) means? What is the use of indexing i.e

for index in xrange(MAXVERSIONS): backup = '%s.%2.2d' % (destpath, index) 

The code:

!/usr/bin/env python

import sys,os, shutil, filecmp

MAXVERSIONS=100
BAKFOLDER = '.bak'

def backup_files(tree_top, bakdir_name=BAKFOLDER):

     top_dir = os.path.basename(tree_top)
    tree_top += os.sep

    for dir, subdirs, files in os.walk(tree_top):

        if os.path.isabs(bakdir_name):
            relpath = dir.replace(tree_top,'')
            backup_dir = os.path.join(bakdir_name, top_dir, relpath)
        else:
            backup_dir = os.path.join(dir, bakdir_name)

        if not os.path.exists(backup_dir):
            os.makedirs(backup_dir)

            subdirs[:] = [d for d in subdirs if d != bakdir_name]
        for f in files:
            filepath = os.path.join(dir, f)
            destpath = os.path.join(backup_dir, f)
                for index in xrange(MAXVERSIONS):
                backup = '%s.%2.2d' % (destpath, index)
                abspath = os.path.abspath(filepath)

                if index > 0:

                    old_backup = '%s.%2.2d' % (destpath, index-1)
                    if not os.path.exists(old_backup): break
                    abspath = os.path.abspath(old_backup)

                    try:
                        if os.path.isfile(abspath) and filecmp.cmp(abspath, filepath, shallow=False):
                            continue
                    except OSError:
                        pass

                try:
                    if not os.path.exists(backup):
                        print 'Copying %s to %s...' % (filepath, backup)
                        shutil.copy(filepath, backup)
                except (OSError, IOError), e:
                    pass

if __name__=="__main__":
    if len(sys.argv)<2:
        sys.exit("Usage: %s [directory] [backup directory]" % sys.argv[0])

    tree_top = os.path.abspath(os.path.expanduser(os.path.expandvars(sys.argv[1])))

    if len(sys.argv)>=3:
        bakfolder = os.path.abspath(os.path.expanduser(os.path.expandvars(sys.argv[2])))
    else:
        bakfolder = BAKFOLDER

    if os.path.isdir(tree_top):
        backup_files(tree_top, bakfolder)
8
  • 1
    No. Ask a specific question. What exactly are we supposed to do? Go through this entire program line by line? Use pdb for that and you can step through the code. Commented Feb 5, 2011 at 11:15
  • 1
    No offense, but no one is going to give you the full explanation of the code. You have to try it yourself first and ask only what is not clear. Commented Feb 5, 2011 at 11:16
  • Whenever in doubt, call upon docs.python.org Commented Feb 5, 2011 at 11:33
  • @minnie: I'm sorry but that isn't much clearer. That whole block does quite a lot. Point out the areas of confusion (like "what does xrange(MAXVERSIONS) mean" etc.) and I'll vote to reopen. Commented Feb 5, 2011 at 12:19
  • Ok. Want to understand what does xrange(MAXVERSION)mean? What is the use of indexing i.e for index in xrange(MAXVERSIONS): backup = '%s.%2.2d' % (destpath, index) Commented Feb 5, 2011 at 12:25

1 Answer 1

3

The script tries to recursively copy the contents of a directory (defaults to current directory) to a backup directory (defaults to .bak in the current directory);

for each filename.ext, it creates a duplicate named filename.ext.00; if filename.ext.00 already exists, it creates filename.ext.01 instead, and so on.

xrange() is a generator which returns all numbers in 0..(MAXVERSION-1), so MAXVERSION controls how many version-suffixes to try, ie how many old versions of the file to keep.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.