Ask Your Question
1

Problem whit ZipFile

asked 2012-07-20 18:00:48 -0500

waltterval gravatar image

updated 2012-07-20 18:02:05 -0500

Hi, I'm reading the post "All you need to analyse the electricity market pt 2" (http://www.whit.com.au/blog/2012/07/all-you-need-to-analyse-the-electricity-market-pt-2/), when I try to run the script "extractzip.py", I get this error

Traceback (most recent call last):

File "H:\PortableAppscom\Documents\PSSE group\PSSPY\extractzip.py", line 10, in <module>

opened_zipfile = ZipFile(zippedfile)

File "C:\Python27\lib\zipfile.py", line 684, in init

self._GetContents()

File "C:\Python27\lib\zipfile.py", line 710, in _GetContents

self._RealGetContents()

File "C:\Python27\lib\zipfile.py", line 720, in _RealGetContents

endrec = _EndRecData(fp)

File "C:\Python27\lib\zipfile.py", line 197, in _EndRecData

fpin.seek(0, 2)

AttributeError: addinfourl instance has no attribute 'seek'

I thik the problem is with "ZipFile(zippedfile)", maybe python has problem whit the filename in variable zippedfile.

I will appreciate your help.

Thanks in advance for you replies. Waltter Valdez

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2012-07-22 11:56:27 -0500

JervisW gravatar image

updated 2012-08-13 05:22:42 -0500

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
edit flag offensive delete link more

Comments

Thank you for your help and yor time, I really appreciate your support

waltterval gravatar imagewaltterval ( 2012-07-23 10:26:07 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

[hide preview]

Question Tools

1 follower

Stats

Asked: 2012-07-20 18:00:48 -0500

Seen: 3,351 times

Last updated: Aug 13 '12