First time here? We are a friendly community of Power Systems Engineers. Check out the FAQ!

Ask Your Question
1

Problem whit ZipFile

asked Jul 21 '12

waltterval gravatar image

updated Jul 21 '12

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

1 answer

Sort by » oldest newest most voted
1

answered Jul 22 '12

JervisW gravatar image

updated Aug 13 '12

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
link

Comments

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

waltterval gravatar imagewaltterval (Jul 23 '12)

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.
Want to format code in your answer? Here is a one minute demo on Youtube

Add Answer

[hide preview]

Question Tools

1 follower

Stats

Asked: Jul 21 '12

Seen: 3,402 times

Last updated: Aug 13 '12