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


Solution 1:[1]

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

Solution 2:[2]

If you need to open a file inside a ZIP archive in text mode, e.g. to pass it to csv.reader, you can do so with io.TextIOWrapper:

import io
import zipfile

with zipfile.ZipFile("files.zip") as zf:
    with io.TextIOWrapper(zf.open("text1.txt"), encoding="utf-8") as f:
        ...

Solution 3:[3]

Since Python 3.8, it's been possible to construct Path objects for zipfile contents, and use their read_text method to read them as text. Since Python 3.9 it's been possible to specify text mode in the path object's open method.


with zipfile.ZipFile('spam.zip') as zf:
    # Create a path object.
    path = zipfile.Path(zf, at='somedir/somefile.txt')

    # Read all the contents (Python 3.8+):
    contents = path.read(encoding='UTF-8')

    # Or open as as file (Python 3.9+):
    with path.open(encoding='UTF-8') as f:
        # Do stuff

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mitch
Solution 2 iafisher
Solution 3 snakecharmerb