zipfile program

This commit is contained in:
gabe venberg 2021-03-01 16:48:37 -06:00
parent 81fe349a23
commit e8375cfc65

36
backupToZip.py Executable file
View file

@ -0,0 +1,36 @@
#! /usr/bin/env python3
usage = "backupToZip: usage backupToZip [directory] backup given directory to a zip file, incrementing filename if name already exists"
import zipfile, os, sys
if len(sys.argv)<1:
print(usage)
sys.exit()
dir = os.path.realpath(sys.argv[1])
#figure out filename based on what already exists.
number = 1
while True:
zipFilename = os.path.basename(dir)+'-'+str(number)+'.zip'
if not os.path.exists(zipFilename):
break
number += 1
print(F'Creating {zipFilename}')
backupZip = zipfile.ZipFile(zipFilename, 'w')
for dirName, subDirList, fileList in os.walk(dir):
#print(f'Adding files in {dirName}')
backupZip.write(dirName)
for fileName in fileList:
#lets not backup other backup files.
newBase = os.path.basename(dir)+'-'
if fileName.startswith(newBase) and fileName.endswith('.zip'):
print(f'skipping {fileName}, is backup file.')
continue
print(f'adding {fileName}')
backupZip.write(os.path.join(dirName, fileName))
backupZip.close()
print('done')