summaryrefslogtreecommitdiff
path: root/Chap9ProjBackupToZip.py
diff options
context:
space:
mode:
authormlot <petri-rush-curvy@duck.com>2025-06-06 13:40:57 -0400
committermlot <petri-rush-curvy@duck.com>2025-06-06 13:40:57 -0400
commit75a42ec54dbf721caa659ddf02c1f46fc2cb4bef (patch)
tree84be794a2481e356a7784557a6f9fb6fbf29cfdd /Chap9ProjBackupToZip.py
initial commit for archivingHEADmain
Diffstat (limited to 'Chap9ProjBackupToZip.py')
-rw-r--r--Chap9ProjBackupToZip.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/Chap9ProjBackupToZip.py b/Chap9ProjBackupToZip.py
new file mode 100644
index 0000000..e9f8e00
--- /dev/null
+++ b/Chap9ProjBackupToZip.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python3
+
+# Chapter 9 Project Backup to Zip
+# Copies a folder recursively into a zip file and names it incrementally.
+
+import os
+import zipfile
+
+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 = number + 1
+
+ print('Creating %s...' % (zipFilename))
+ backupZip = zipfile.ZipFile(zipFilename, 'w')
+
+ for foldername, subfolders, filenames in os.walk(folder):
+ print('Adding files in ' + foldername + '...')
+ backupZip.write(foldername)
+ for filename in filenames:
+ newBase = os.path.basename(folder) + '_'
+ if filename.startswith(newBase) and filename.endswith('.zip'):
+ continue
+ backupZip.write(os.path.join(foldername, filename))
+
+ backupZip.close()
+ print('Done')
+
+
+backupToZip('/Users/mlotspaih/Documents/waffle')