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 :
- import ‘_zipfile’ module
- Create one new object by passing the zip file path and read (r) or write (w) mode like _zipfile.ZipFile(‘file_path’,‘r’) .
- It will return list of all files inside that zip file.
- Run one ‘_for’ loop and print out all the names of the files.
- Finally, close the object by using _.close() method. ____