0

您好我的错误是在生成 zip 文件时产生的。你能告诉我应该怎么做吗?

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

完整代码如下:

 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)
4

3 回答 3

3

那可能是zipstream而不是zipf。因此,将其替换为zipstream它可能会起作用。

于 2011-04-15T07:27:54.693 回答
1

我没看到你在哪里声明 zipf?

压缩文件?Senthil Kumaran 可能对 zipstream 是正确的,因为您在 while 循环之前在 zipstream 上 seek(0) 以读取神秘变量的块。

编辑:

几乎可以肯定变量是 zipstream。

zipfile docs

类 zipfile.ZipFile(file[, mode[, compression[, allowZip64]]])

打开一个 ZIP 文件,其中 file 可以是文件的路径(字符串)或类似文件的对象。mode 参数应该是“r”来读取现有文件,“w”来截断并写入新文件,或者“a”来附加到现有文件。如果 mode 是 'a' 并且 file 是指现有的 ZIP 文件,那么会向其中添加其他文件。如果文件不引用 ZIP 文件,则将新的 ZIP 存档附加到文件中。这用于将 ZIP 存档添加到另一个文件(例如 python.exe)。

你的代码:

zipsteam=StringIO.StringIO() 

使用 StringIO 创建一个类似文件的对象,它本质上是一个“内存文件”阅读更多内容docs

file = zipfile.ZipFile(zipstream,w)

以 'w' 模式打开带有类似 zipstream 文件的对象的 zipfile

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()

使用 addFile 方法检索检索到的数据并将其写入类文件对象并返回。这些变量有点令人困惑,因为您将 zipfile 传递给别名为 zipstream 的 addFile 方法(令人困惑,因为我们使用 zipstream 作为 StringIO 类文件对象)。无论如何,zipfile 被返回,并关闭以确保所有内容都被“写入”。

它被写入我们的“内存文件”,我们现在寻求索引 0

zipstream.seek(0)

在做了一些头文件之后,我们终于到达了 while 循环,它将以块的形式读取我们的“内存文件”

while True:
    buf=zipstream.read(2048)
    if buf=="": break
    self.response.out.write(buf)
于 2011-04-15T07:27:49.190 回答
0

您需要声明:

global zipf

就在你之后 def get(self):

线。你正在修改一个全局变量,这是 python 知道你在做什么的唯一方法。

于 2011-04-15T07:26:17.507 回答