Reputation: 431
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
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
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