2011-04-15 117 views
0

你好 我的错误是在生成zip文件时产生的。你能告诉我该怎么办?NameError:未定义全局名称

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) 
+1

'if not self.request.get('count')==''' - 认真吗? '不a == b'通常应该是'a!= b' – ThiefMaster 2011-04-23 00:17:31

+0

@ThiefMaster感谢您的代码审查。我基本上是从Java背景学习python,所以我仍然对'is'和'=='有所疑惑' – 2011-09-28 22:13:00

+1

'is'有点像Java的'=='对象(测试a和b是否相同) ,'=='更像'a.Equals(b)'。如果你真的想要测试两个对象是否相同或者测试是否有*无*(如果你只想测试一个虚假值,只需测试'not a'),那么基本上只使用'is' – ThiefMaster 2011-09-29 06:34:44

回答

3

那很可能是zipstream而不是zipf。所以用zipstream代替它,它可能会工作。

+0

感谢您的回答。我从http://www.tareandshare.com/2008/09/28/Zip-Google-App-Engine-GAE/获取代码,它说zipf。你可以解释吗? – 2011-04-15 08:13:34

+1

@Web人 - 代码有问题,并且可能还不完整,因为您必须在适当的类中使用这些方法。 – 2011-04-15 08:41:59

+0

我们修复了它,并且现在可以使用该压缩并且可以运行(http://www.koolbusiness.com/list.kmz) – 2011-04-16 05:58:55

0

需要声明:

global zipf 

def get(self):

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

+0

我猜他不想在这里修改全局。它只是说全局的,因为错误在一个方法中,并且在本地或全局范围内找不到。 – DTing 2011-04-15 07:33:30

+0

这是真的。我没有看到zipf甚至没有被宣布。他正在寻找zipfile。 – Evan 2011-04-15 07:40:04

+0

它说zipf,我没有在这里看到声明:http://www.tareandshare.com/2008/09/28/Zip-Google-App-Engine-GAE/ – 2011-04-15 08:14:41

1

我看不到您声明zipf的位置?

zipfile? Senthil Kumaran可能是正确的zipstream,因为你在while循环之前寻找(0)zipstream来读取神秘变量的块。

编辑:

几乎可以肯定的变量是zipstream。

zipfile docs

类zipfile.ZipFile(文件[,模式[,压缩[,allowZip64]]])

Open a ZIP file, where file can be either a path to a file (a string) or a file-like object. The mode parameter should be 'r' to read an existing file, 'w' to truncate and write a new file, or 'a' to append to an existing file. If mode is 'a' and file refers to an existing ZIP file, then additional files are added to it. If file does not refer to a ZIP file, then a new ZIP archive is appended to the file. This is meant for adding a ZIP archive to another file (such as python.exe).

您的代码:

zipsteam=StringIO.StringIO() 

创建使用StringIO的文件类对象,实质上是一个“内存文件”,在docs

file = zipfile.ZipFile(zipstream,w) 

打开zip文件与“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() 

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

这是写我们的“记忆文件”,这是我们现在寻求指数0

zipstream.seek(0) 

,做一些头的东西之后,我们终于到达while循环,将阅读我们的“记忆文件”在块

while True: 
    buf=zipstream.read(2048) 
    if buf=="": break 
    self.response.out.write(buf) 
+0

感谢您的答案。我从代码获取的地方是http://www.tareandshare.com/2008/09/28/Zip-Google-App-Engine-GAE/ – 2011-04-15 08:13:56

相关问题