get file list of files contained in a zip file


I have a zip archive: Inside it is one txt file, the name of which I do not know. I was taking a look at Python’s zipfile module ( ), but couldn’t make too much sense of what I’m trying to do.

How would I do the equivalent of ‘double-clicking’ the zip file to get the txt file and then use the txt file so I can do:

>>> f = open('my_txt_file.txt','r')
>>> contents =
Asked By: David542



import zipfile


You can see the documentation here. In particular, the namelist() method will give you the names of the zip file members.

What you need is ZipFile.namelist() that will give you a list of all the contents of the archive, you can then do a'filename_you_discover') to get the contents of that file.

Answered By: user177800
import zipfile

# zip file handler  
zip = zipfile.ZipFile('')

# list available files in the container
print (zip.namelist())

# extract a specific file from the zip container
f ="file_inside_zip.txt")

# save the extraced file 
content =
f = open('file_inside_zip.extracted.txt', 'wb')
Answered By: J. Ceron
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.