Program to backing up a given Folder into a ZIP File

6. b) Develop a program to backing up a given Folder (Folder in a current working directory)
into a ZIP File by using relevant modules and suitable methods.

# Write a python program to create ZIP file of a perticular folder which contains several files inside it
import os
import sys
import pathlib
import zipfile

dirName=input("Enter Directory name that you want to backup")
if not os.path.isdir(dirName):
    print("Directory",dirName,"doent exist")
    sys.exit(0)
curDirectory=pathlib.Path(dirName)
with zipfile.ZipFile("myzip.zip",mode="w")as archive:
    for file_path in curDirectory.rglob("*"):
        archive.write(file_path,arcname=file_path.relative_to(curDirectory))
if os.path.isfile("myzip.zip"):
    print("Archive","myzip.zip","created succesfully1")
else:
    print("Error in creating")

Leave a Reply

Your email address will not be published. Required fields are marked *