Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 applications such as file management, data processing, file conversion, and backup & archive of files. In Python, we have built-in functions like namelist() and infolist() that can be used to read and print all files from a zip file.
Syntax
The basic syntax for working with zip files ?
import zipfile
with zipfile.ZipFile("filename.zip", "r") as zip_file:
file_list = zip_file.namelist()
The ZipFile() method accepts two parameters ?
filename.zip The name of the zip file to read
"r" Read mode to access the contents of the zip file
Key Methods
namelist()
Returns a list of all file names in the zip archive ?
file_names = zip_file.namelist()
infolist()
Returns a list of ZipInfo objects containing detailed information about each file ?
file_info = zip_file.infolist()
Example 1: Using namelist()
This example demonstrates how to get a simple list of file names using namelist() ?
import zipfile
import os
# Create a sample zip file for demonstration
with zipfile.ZipFile('sample.zip', 'w') as zip_file:
# Create some sample files and add to zip
zip_file.writestr('document1.txt', 'This is document 1 content')
zip_file.writestr('document2.txt', 'This is document 2 content')
zip_file.writestr('readme.md', 'This is readme content')
# Read and print all files from the zip
with zipfile.ZipFile('sample.zip', 'r') as zip_file:
file_list = zip_file.namelist()
print("Files in the zip archive:")
for file_name in file_list:
print(f"- {file_name}")
Files in the zip archive: - document1.txt - document2.txt - readme.md
Example 2: Using infolist()
This example shows how to get detailed file information using infolist() ?
import zipfile
import datetime
# Read detailed file information from the zip
with zipfile.ZipFile('sample.zip', 'r') as zip_file:
file_info_list = zip_file.infolist()
print("Detailed file information:")
for file_info in file_info_list:
# Get file modification date
date_time = datetime.datetime(*file_info.date_time)
print(f"File: {file_info.filename}")
print(f" Size: {file_info.file_size} bytes")
print(f" Compressed Size: {file_info.compress_size} bytes")
print(f" Modified: {date_time}")
print()
Detailed file information: File: document1.txt Size: 25 bytes Compressed Size: 27 bytes Modified: 2024-01-01 00:00:00 File: document2.txt Size: 25 bytes Compressed Size: 27 bytes Modified: 2024-01-01 00:00:00 File: readme.md Size: 22 bytes Compressed Size: 24 bytes Modified: 2024-01-01 00:00:00
Comparison
| Method | Returns | Best For |
|---|---|---|
namelist() |
List of file names (strings) | Simple file listing |
infolist() |
List of ZipInfo objects | Detailed file information |
Conclusion
Python's zipfile module provides easy methods to read zip file contents. Use namelist() for simple file name listing and infolist() when you need detailed file information like size and modification dates.
