0 votes
1 view
in Python by (16.1k points)

How can I create a zip archive of a directory structure in Python?

1 Answer

0 votes
by (107k points)

To create a zip archive of a directory you can use the shutil.make_archive() the function which supports the zip format. Below is the code for the same. 

import shutil

shutil.make_archive(output_filename, 'zip', dir_name)

Another way to create a zip archive of a directory in Python is by using the zipfile below is the code that tells you how to use it:-

#!/usr/bin/env python

import os

import zipfile

def zipdir(path, ziph):

    for root, dirs, files in os.walk(path):

          for file in files:

                ziph.write(os.path.join(root, file)) 

if __name__ == '__main__':  #This is driver code

       zipf = zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED)

       zipdir('tmp/', zipf)


Related questions

+1 vote
1 answer
0 votes
2 answers
asked Jul 6, 2019 in Python by Sammy (47.8k points)
0 votes
1 answer
+1 vote
1 answer
asked May 18, 2019 in Python by Nigam (4.1k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !