![]() |
VOOZH | about |
Python provides the built-in zipfile module to work with ZIP files. A ZIP file compresses multiple files into a single archive without data loss, making it useful for saving storage space, faster file transfer and better organization of related files.
The zipfile module can be used for various purposes, including:
1. Read ZIP Files: Python allows you to access and extract the contents of a compressed archive without manually unzipping it.
Output
File Name Modified Size
file.txt 2026-01-17 10:35:16 72
Explanation:
2. Extracting a ZIP File: Extracting means unpacking all or specific files from a compressed archive so they can be used normally.
Output
Extracted files:
file.txt
Explanation:
3. Writing to a ZIP File: It allows you to create a new ZIP archive or add files to an existing one.
Note: This code creates new_file.zip and adds info.txt and data.txt to it. No output is shown, but the ZIP file is created in the script’s folder.
Explanation:
4. Appending Files to an Existing ZIP: Python allows you to add files to an existing ZIP archive without overwriting it by using append mode ('a').
Note: This code adds test1.txt to the ZIP file "file.zip". Nothing is printed, but the ZIP now contains the new file.
Explanation:
5. Reading File Contents of ZIP File: Python allows reading the contents of a file inside a ZIP without extracting it. This is useful when you want to process files directly from the archive.
Output
Welcome to GeeksforGeeks!
Explanation: