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を宣言する場所がわかりませんか?

ZIPファイル?謎の変数のチャンクを読み取る while ループの前に zipstream で seek(0) を使用しているため、Senthil Kumaran はおそらく zipstream で正しいでしょう。

編集:

ほぼ確実に、変数は zipstream です。

zipfile docs:

クラスzipfile.ZipFile(ファイル[、モード[、圧縮[、許可Zip64]]])

ZIP ファイルを開きます。file は、ファイルへのパス (文字列) またはファイルのようなオブジェクトのいずれかです。モード パラメータは、既存のファイルを読み取る場合は 'r'、新しいファイルを切り捨てて書き込む場合は 'w'、または既存のファイルに追加する場合は 'a' である必要があります。モードが 'a' で、file が既存の ZIP ファイルを参照する場合、追加のファイルがそれに追加されます。file が 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 メソッドを使用して取得し、取得したデータをファイルのようなオブジェクトに書き込み、それを返します。addFile メソッドに zipfile を渡すと、zipstream としてエイリアス化されるため、変数は少し混乱します (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 に答える