Advertisement
Guest User

Untitled

a guest
May 23rd, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. #! python3
  2. # backupToZip.py
  3. # Copies an entire folder and its contents into
  4. # a zip file whose filename increments.
  5.  
  6. import zipfile, os
  7.  
  8. def backupToZip(folder):
  9. # Backup the entire contents of "folder" into a zip file.
  10.  
  11. folder = os.path.abspath(folder) # make sure folder is absolute
  12.  
  13. # Figure out the filename this code should used based on
  14. # what files already exist.
  15. number = 1
  16. while True:
  17. zipFilename = os.path.basename(folder) + '_' + str(number) + '.zip'
  18. if not os.path.exists(zipFilename):
  19. break
  20. number = number + 1
  21.  
  22. # Create the zip file.
  23. print('Creating %s...' % (zipFilename))
  24. backupZip = zipfile.ZipFile(zipFilename, 'w')
  25.  
  26. # Walk the entire folder tree and compress the files in each folder.
  27. for foldername, subfolders, filenames in os.walk(folder):
  28. print('Adding files in %s...' % (foldername))
  29. # Add the current folder to the ZIP file.
  30. backupZip.write(foldername)
  31.  
  32. # Add all the files in this folder to the ZIP file.
  33. for filename in filenames:
  34. if filename.startswith(os.path.basename(folder) + '_') and filename.endswith('.zip'):
  35. continue # don't backup the backup ZIP files
  36. backupZip.write(os.path.join(foldername, filename))
  37. backupZip.close()
  38. print('Done.')
  39.  
  40.  
  41. backupToZip('C:\\delicious')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement