ajkey94 ajkey94 - 1 month ago 16
Python Question

How do I read text files within a zip file?

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

words


and
"text2.txt":

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

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