0% found this document useful (0 votes)
6 views2 pages

Folder Backup To ZIP Program

The document outlines a Python program that creates a ZIP backup of a specified folder while avoiding duplication of existing ZIP files. It defines a function 'backupToZip' that generates a unique ZIP filename, traverses the folder structure, and writes the files to the new ZIP file. The expected output is a ZIP file containing all files from the folder, with console messages indicating the progress of the backup process.

Uploaded by

charanbhat5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Folder Backup To ZIP Program

The document outlines a Python program that creates a ZIP backup of a specified folder while avoiding duplication of existing ZIP files. It defines a function 'backupToZip' that generates a unique ZIP filename, traverses the folder structure, and writes the files to the new ZIP file. The expected output is a ZIP file containing all files from the folder, with console messages indicating the progress of the backup process.

Uploaded by

charanbhat5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Python Program: Folder Backup to ZIP

Aim of the Program:

To create a ZIP backup of a folder using Python. This program automatically names the backup and

excludes previously created ZIP backups to avoid duplication.

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

print(f'Creating {zipFilename}...')
backupZip = zipfile.ZipFile(zipFilename, 'w')

for foldername, subfolders, filenames in os.walk(folder):


print(f'Adding files in {foldername}...')
for filename in filenames:
if filename.startswith(os.path.basename(folder)) and
filename.endswith('.zip'):
continue
backupZip.write(os.path.join(foldername, filename))

backupZip.close()
print('Done.')

Procedure:

1. Import zipfile and os modules.

2. Define function 'backupToZip' that takes folder path.

3. Generate a unique ZIP filename (e.g., folder_1.zip).

4. Walk through the folder structure using os.walk().

5. Skip old backup ZIPs to avoid re-backing them.

6. Write files to the new ZIP file.


7. Close the ZIP and display completion message.

Expected Output:

A file like 'MyFolder_1.zip' will be created containing all files from the folder.

Console Output:

Creating MyFolder_1.zip...

Adding files in C:\MyFolder...

Done.

You might also like