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(‘filepath’,‘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. __
Similar tutorials :
- zip function in python and how to use it
- Python program to count the number of words in a file
- Python 3 program to count the number of blank spaces in a file
- Python tutorial to remove duplicate lines from a text file
- Python program to count the total number of lines in a file
- Python program to rename a directory or file