Examining Zipfile Contents

suggest change

There are a few ways to inspect the contents of a zipfile. You can use the printdir to just get a variety of information sent to stdout

with zipfile.ZipFile(filename) as zip:
    zip.printdir()

    # Out:
    # File Name                                             Modified             Size
    # pyexpat.pyd                                    2016-06-25 22:13:34       157336
    # python.exe                                     2016-06-25 22:13:34        39576
    # python3.dll                                    2016-06-25 22:13:34        51864
    # python35.dll                                   2016-06-25 22:13:34      3127960
    # etc.

We can also get a list of filenames with the namelist method. Here, we simply print the list:

with zipfile.ZipFile(filename) as zip:
    print(zip.namelist())

# Out: ['pyexpat.pyd', 'python.exe', 'python3.dll', 'python35.dll', ... etc. ...]

Instead of namelist, we can call the infolist method, which returns a list of ZipInfo objects, which contain additional information about each file, for instance a timestamp and file size:

with zipfile.ZipFile(filename) as zip:
    info = zip.infolist()
    print(zip[0].filename)
    print(zip[0].date_time)
    print(info[0].file_size)

# Out: pyexpat.pyd
# Out: (2016, 6, 25, 22, 13, 34)
# Out: 157336

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents