Search code examples
pythonzippython-zipfile

get file list of files contained in a zip file


I have a zip archive: my_zip.zip. Inside it is one txt file, the name of which I do not know. I was taking a look at Python's zipfile module ( http://docs.python.org/library/zipfile.html ), 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 = f.read()

Solution

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