2011-04-15 115 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

相關問題