title: Versioning file name using Python
body: <p>Basically when I download a file, it should check whether the file exists or not. If not exits rename the file with the version-0 else if exists rename the file with the next iterating version (eg- it should be file-name-version-1)</p>
  
  <p>Here is what I have tried:</p>
  
  <pre><code>def VersionFile(file_spec, vtype='copy'):
      import os, shutil
  
      ok = 0
      if os.path.isfile(file_spec):
          # or do other error checking...
          if vtype not in ('copy', 'rename'):
              vtype = 'copy'
  
          # determine root file name so the extension doesn't get longer and longer...
          n, e = os.path.splitext(file_spec)
  
          # is e an integer?
          try:
              num = int(e)
              root = n
          except ValueError:
              root = file_spec
  
          # find next available file version
          for i in xrange(100):
              new_file = '%s_V.%d' % (root, i)
              if not os.path.isfile(new_file):
                  if vtype == 'copy':
                      shutil.copy(file_spec, new_file)
                  else:
                      os.rename(file_spec, new_file)
                  ok = 1
                  break
      return ok
  
  if __name__ == '__main__':
      # test code (you will need a file named test.txt)
      print VersionFile('test.txt') # File is exists in the directory
      print VersionFile('alpha.txt') # File not exists in the directory
  </code></pre>
  
  <p>Above code is going to work only after downloading the file it will explicitly check and Renaming the file with version if exists.</p>
  
  <p>I want in such a way it should check implicitly.</p>