Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! python3
- #! backs folder up to a zip file and the version nr increments everytime its created
- import zipfile, os
- def backupToZip(folder):
- folder = os.path.abspath(folder)
- number = 1
- while True:
- zipFilename = os.path.basename(folder) + '_' + str(number) + '.zip'
- if not os.path.exists(zipFilename):
- break
- number += 1
- #TODO - Create a zip file
- print('Creating the %s ...' % (zipFilename))
- backupZip = zipfile.ZipFile(zipFilename, 'w')
- #todo - Walk the entire folder and comporess the files in each folder
- for foldername, subfolders, filenames in os.walk(folder):
- print(('Adding files in %s...') % (foldername))
- #add the current folder to the ZIP file
- backupZip.write(foldername)
- #add all the files in this folder to the zip file
- for filename in filenames:
- newBase = os.path.basename(folder) + '_'
- if filename.startswith(newBase) and filename.endswith('.zip'):
- continue # dont backup the backup zip files
- backupZip.write(os.path.join(foldername, filename))
- backupZip.close()
- print('done')
- backupToZip('Downloads')
Add Comment
Please, Sign In to add comment