-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch9_backupToZip2.py
38 lines (31 loc) · 1.17 KB
/
ch9_backupToZip2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#! python3
# backupToZip.py - copies entire folder and its contents into
# a zip file whose filename increments
import zipfile, os
def backupToZip(folder):
# Backup the entire contents of a "folder" into a zip file.
folder = os.path.abspath(folder)
# Get zip filename
number = 1
while True:
zipFileName = os.path.basename(folder) + '_' + str(number) + '.zip'
if not os.path.exists(zipFileName):
break
number += 1
# Create zip file
print('creating {} ...'.format(zipFileName))
backupZipFile = zipfile.ZipFile(zipFileName, 'w')
# Walk entire tree and compress into folder
for fname, subfolders, files in os.walk(folder):
print('Adding files in {} ...'.format(fname))
# Add current folder to zip
backupZipFile.write(fname)
# Add all files in this folder to zip
for file in files:
newBase = os.path.basename(folder) + '_'
if file.startswith(newBase) and file.endswith('.zip'):
continue
backupZipFile.write(os.path.join(fname, file))
backupZipFile.close()
print('Done')
backupToZip('C:\\Users\\hkarn\\Downloads\\example')