Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hi, from your description I was able to find a mistake in the code on the blog.

the urlopen function returns a file-like object, which behaves the same as a file. Except that it doesn't have a seek function.

Fixing the mistake

Use the StringIO library to fix the problem. ZipFile needs a file-like object. But the urlopen gives us something that is missing: seek.

StringIO to the rescue

StringIO turns an ordinary string into a file-like object, complete with seek method. Here is what that looks like:

from StringIO import StringIO

generator = "Red Hills Nuclear Plant"

filelike = StringIO(generator)
contents = filelike.read() # read the whole 'file'
filelike.seek(0) # re-wind the file back to beginning

That example is a short demonstration. Try it line by line to see what is happening. This is how I will re-write the extractzip.py example:

from __future__ import with_statement
from urllib2 import urlopen
from StringIO import StringIO
from zipfile import ZipFile

PRICE_REPORTS_URL = 'http://www.nemweb.com.au/Reports/CURRENT/Public_Prices'
ZIP_URL = '/PUBLIC_PRICES_201207040000_20120705040607.ZIP'

# zippedfile is now one long string.
zippedfile = urlopen(PRICE_REPORTS_URL + ZIP_URL).read()

# StringIO turns the string into a real file-like object.
opened_zipfile = ZipFile(StringIO(zippedfile))
filenames = opened_zipfile.namelist()

print filenames

Hi, from your description I was able to find a mistake in the code on the blog.

the urlopen function returns a file-like object, which behaves the same as a file. Except that it doesn't have a seek function.

Fixing the mistake

Use the StringIO library to fix the problem. ZipFile needs a file-like object. But the urlopen gives us something that is missing: seek.

StringIO to the rescue

StringIO turns an ordinary string into a file-like object, complete with seek method. Here is what that looks like:

from StringIO import StringIO

generator = "Red Hills Nuclear Plant"

filelike = StringIO(generator)
contents = filelike.read() # read the whole 'file'
filelike.seek(0) # re-wind the file back to beginning

That example is a short demonstration. Try it line by line to see what is happening. This is how I will re-write the extractzip.py example:

from __future__ import with_statement
from urllib2 import urlopen
from StringIO import StringIO
from zipfile import ZipFile

PRICE_REPORTS_URL = 'http://www.nemweb.com.au/Reports/CURRENT/Public_Prices'
ZIP_URL = '/PUBLIC_PRICES_201207040000_20120705040607.ZIP'

# zippedfile is now one long string.
zippedfile = urlopen(PRICE_REPORTS_URL + ZIP_URL).read()

# StringIO turns the string into a real file-like object.
opened_zipfile = ZipFile(StringIO(zippedfile))
filenames = opened_zipfile.namelist()

print filenames

Updated

Hi, from your description I was able to find a mistake in the code on the blog.

the urlopen function returns a file-like object, which behaves the same as a file. Except that it doesn't have a seek function.

Fixing the mistake

Use the StringIO library to fix the problem. ZipFile needs a file-like object. But the urlopen gives us something that is missing: seek.

StringIO to the rescue

StringIO turns an ordinary string into a file-like object, complete with seek method. Here is what that looks like:

from StringIO import StringIO

generator = "Red Hills Nuclear Plant"

filelike = StringIO(generator)
contents = filelike.read() # read the whole 'file'
filelike.seek(0) # re-wind the file back to beginning

That example is a short demonstration. Try it line by line to see what is happening. This is how I will re-write the extractzip.py example:

from __future__ import with_statement
from urllib2 import urlopen
from StringIO import StringIO
from zipfile import ZipFile

PRICE_REPORTS_URL = 'http://www.nemweb.com.au/Reports/CURRENT/Public_Prices'
ZIP_URL = '/PUBLIC_PRICES_201207040000_20120705040607.ZIP'

# zippedfile is now one long string.
zippedfile = urlopen(PRICE_REPORTS_URL + ZIP_URL).read()

# StringIO turns the string into a real file-like object.
opened_zipfile = ZipFile(StringIO(zippedfile))
filenames = opened_zipfile.namelist()

print filenames

Updated