Python Program to Read and printing all files from a zip file


Multiple files are compressed together and stored in a single file format called a zip file. There are some applications such as file management, data processing, file conversion, and backup & archive of files. In Python, we have zip in-built functions like infolist() and namelist() that can be used to Read and printing all files from a zip file. We utilize the predefined method 'namelist()' or 'infolist' to read the data within the zip folder in order to output all of the files that are currently present inside the zip file.

Input

// This is a zip folder

Output

The list of files inside the zip file −


Syntax

The following syntax is used in the example are −

with zipfile.Zipfile("filename.zip", "r")

The above predefined method defines the read and writes in a zip file. It accepts two parameters −

  • filename.zip − Mention the name of a zip file.

  • r − The r is a reading mode to read the list of file names inside the zip file

namelist()

The namelist() is a predefined method in Python to achieve the list of files from the zip.

infolist()

The infolist() method in Python contains all the information of the zip file.

Example 1

In the following example, we will start the program by using ‘zipfile’ module. Then open the zip file using a predefined method named with zipfile.Zipfile() which accepts two parameters i.e. ‘Combine.zip’(Zip file) and ‘r’(read the list of all files of zip). Then use the predefined method namelist() in the variable named ‘file_list’ that will find the list of files inside the zip file. Finally, print the result with the help of the variable ‘file_list’.

import zipfile
with zipfile.ZipFile('Combine.zip', 'r') as zip_file:
   file_list = zip_file.namelist()
   print("The file list are:",file_list)

Output

The file list are: ['mydoc.txt', 'Namebox.txt', 'tutebox.txt']

Example 2

In this program, we will start the program by importing the module zipfile. Then open the zip file using ‘with zipfile.Zipfile()’ method. This method accepts two parameters- Combine.zip(Mention the zip file name) and r(The ‘r’ mode in file handling read all the existing files of a zip file). To get the list of all files, the predefined method ‘infolist()’ is used. Next, create the empty list variable named file_list to hold the name of all files inside the zip file. Moving ahead to use the for loop to extract the file information from a zip file and add the list of files by using the append() method. Finally, print the list of all files from the zip folder with the help of file_list.

import zipfile
with zipfile.ZipFile('Combine.zip', 'r') as zip_file:
   file_info_list = zip_file.infolist()
   file_list = []
   for file_info in file_info_list:
      file_list.append(file_info.filename)
   print("The file list are-",file_list)

Output

The file list are: ['mydoc.txt', 'Namebox.txt', 'tutebox.txt']

Conclusion

We discussed how the module helps to read and extract the files from the Zip file. We read the zip file’s contents and extract particular files or directories by using the "ZipFile" class. Then we saw the difference to find the list of all files from the zip file by using namelist()(example 1) and infolist()(example 2) methods.

Updated on: 01-Jun-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements