NameError: global name is not defined

Niklas R. picture Niklas R. · Apr 15, 2011 · Viewed 11.3k times · Source

Hello My error is produced in generating a zip file. Can you inform what I should do?

main.py", line 2289, in get
    buf=zipf.read(2048)
NameError: global name 'zipf' is not defined

The complete code is as follows:

 def addFile(self,zipstream,url,fname):
     # get the contents          
     result = urlfetch.fetch(url)

     # store the contents in a stream
     f=StringIO.StringIO(result.content)
     length = result.headers['Content-Length']
     f.seek(0)

     # write the contents to the zip file
     while True:
       buff = f.read(int(length))
       if buff=="":break
       zipstream.writestr(fname,buff)
       return zipstream

 def get(self):   
    self.response.headers["Cache-Control"] = "public,max-age=%s" % 86400
    start=datetime.datetime.now()-timedelta(days=20)
    count = int(self.request.get('count')) if not self.request.get('count')=='' else 1000        
    from google.appengine.api import memcache
    memcache_key = "ads"
    data = memcache.get(memcache_key)
    if data is None:
      a= Ad.all().filter("modified >", start).filter("url IN", ['www.koolbusiness.com']).filter("published =", True).order("-modified").fetch(count)
      memcache.set("ads", a)  
    else:
      a = data
    dispatch='templates/kml.html'
    template_values = {'a': a , 'request':self.request,}
    path = os.path.join(os.path.dirname(__file__), dispatch)
    output = template.render(path, template_values)    
    self.response.headers['Content-Length'] = len(output)    
    zipstream=StringIO.StringIO()
    file = zipfile.ZipFile(zipstream,"w")
    url = 'http://www.koolbusiness.com/list.kml'
    # repeat this for every URL that should be added to the zipfile
    file =self.addFile(file,url,"list.kml")
    # we have finished with the zip so package it up and write the directory
    file.close()
    zipstream.seek(0)
    # create and return the output stream
    self.response.headers['Content-Type'] ='application/zip'
    self.response.headers['Content-Disposition'] = 'attachment; filename="list.kmz"' 
    while True:
      buf=zipf.read(2048)
      if buf=="": break
    self.response.out.write(buf)

Answer

Senthil Kumaran picture Senthil Kumaran · Apr 15, 2011

That is probably zipstream and not zipf. So replace that with zipstream and it might work.