ajkey94 ajkey94 - 3 years ago 165
Python Question

How do I read text files within a zip file?

So say I have a zip file named "files.zip"
it contains



other words

How do I tell python to open and read the text1.txt file? I know that usually to open a text file outside of a zip file I would just do this:

file = open('text1.txt','r')

Answer Source

You can use the zipfile module like so:

zip = zipfile.ZipFile('test.zip')
file = zip.read('text1.txt')

Don't forget to import zipfile module: import zipfile

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download