List all the files in a Zip file using Python 3

List all the files in a zip file using python 3 :

In this tutorial, we will learn how to find out all the files of a zip file. For that, we will use ‘zipfile‘ library which is already available in the ‘python‘ package. We will have to use ‘import zipfile’. Using this module, we can create, read, write,append and list all the files from a Zip file.

One thing to be noted that this module cannot be used with multi disk zip files. Let’s take a look in to the program :

import zipfile

zip_file = zipfile.ZipFile('compressed_file.zip','r')

for name in zip_file.namelist():
    print ('%s' % (name))

zip_file.close()

Steps Used :

1. import ‘zipfile‘ module
2. Create one new object by passing the zip file path and read (r) or write (w) mode like zipfile.ZipFile(‘file_path’,’r’) .
3. It will return list of all files inside that zip file.
4. Run one ‘for‘ loop and print out all the names of the files.
5. Finally, close the object by using .close() method.

Leave a Reply