Sliwa
Sliwa

Reputation: 61

Unable to read zipped file from url

I am unable to open a zipped file from a web.

from urllib.request import urlopen
from io import BytesIO
from zipfile import ZipFile
url = "http://..../craft.zip"
file = urlopen(url).read()
file = BytesIO(file)
document = ZipFile(file)
content = document.read('MASTER.txt')

And when I try to print some data, I got a bunch of numbers. There are other txt files in that zip, and when I replace the file name in content, I got the same output. Although I read py3k: How do you read a file inside a zip file as text, not bytes?, I don't know how to fix it.

Upvotes: 1

Views: 283

Answers (1)

Sliwa
Sliwa

Reputation: 61

The problem was with method of zipfile:

from urllib.request import urlopen
from io import BytesIO
from zipfile import ZipFile

url = "http://....craft.zip"
file = urlopen(url).read()
file = BytesIO(file)
document = ZipFile(file)
content = document.open('MASTER.txt', "r")
for line in content:
        print(line)

This code fixed my problem and I was able to look up data in the zipfile. Read has been replaced by open.

Upvotes: 1

Related Questions