ajkey94
ajkey94

Reputation: 431

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

Upvotes: 16

Views: 36666

Answers (3)

snakecharmerb
snakecharmerb

Reputation: 55933

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.

import zipfile

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_text(encoding='UTF-8')

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

Upvotes: 5

iafisher
iafisher

Reputation: 1008

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:
        ...

Upvotes: 28

solusipse
solusipse

Reputation: 587

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

Upvotes: 17

Related Questions