get file list of files contained in a zip file

David542 picture David542 · Jan 13, 2012 · Viewed 70.7k times · Source

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()

Answer

user177800 picture user177800 · Jan 13, 2012

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.