Library contents

Python ZipFile.infolist

Iterate over the archive directory to inspect the members in stored order.

A simple example

ZipFile.infolist

  1. Start with Archive containing only a.txt
  2. Do this Read its entry directory
  3. Result One entry named a.txt
An archive entry describes a member. Reading its contents is a separate step.

Syntax

Python syntax
archive.infolist()
Run these examples in Python

Save one example at a time as lesson.py. Use Python 3.9 or later.

python3 lesson.py

List the entries in a receipt archive

An office keeps individual receipts inside one archive. Work with the archive and its named entries so a clerk can locate the intended receipt without treating the archive itself as a single receipt.

Resulttea.txt
lesson.py
from io import BytesIO
from zipfile import ZipFile, ZIP_STORED

store = BytesIO()
with ZipFile(store, "w", compression=ZIP_STORED) as archive:
    archive.writestr("tea.txt", "paid")

store.seek(0)
with ZipFile(store) as archive:
    for entry in archive.infolist():
        print(entry.filename)
Output
tea.txt

Each member has a ZipInfo record with its name and sizes.

Advertisements

Inspect a choir archive

A teacher saves the bytes for the word alto in an uncompressed archive member. Read the first entry name, retaining the complete folder prefix. Which archive path appears?

Givenparts/alto.txt · 4 bytes
Worked resultparts/alto.txt

The archive contains one stored member. Its entry metadata and contents can be inspected independently.

Inspect a workshop archive

A tutor saves the bytes for glaze in a second uncompressed archive member. Read the first entry name, retaining the complete folder prefix. Which archive path appears?

Givenstudio/tag.txt · 5 bytes
Worked resultstudio/tag.txt

The archive contains one stored member. Its entry metadata and contents can be inspected independently.

What to watch for

For an archive supplied by someone else, check expected member names and size limits before reading large payloads.

Documentation for ZipFile.infolist