Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #10261 > unrolled thread

Pipe in the "return" statement

Started byArchard Lias <archardlias@googlemail.com>
First post2011-07-25 05:00 -0700
Last post2011-07-26 11:22 -0400
Articles 2 on this page of 22 — 13 participants

Back to article view | Back to comp.lang.python


Contents

  Pipe in the "return" statement Archard Lias <archardlias@googlemail.com> - 2011-07-25 05:00 -0700
    Re: Pipe in the "return" statement Ian Collins <ian-news@hotmail.com> - 2011-07-26 00:03 +1200
      Re: Pipe in the "return" statement Archard Lias <archardlias@googlemail.com> - 2011-07-25 07:16 -0700
        Re: Pipe in the "return" statement Billy Mays <81282ed9a88799d21e77957df2d84bd6514d9af6@myhashismyemail.com> - 2011-07-25 10:31 -0400
          Re: Pipe in the "return" statement Ethan Furman <ethan@stoneleaf.us> - 2011-07-25 13:07 -0700
            Re: Pipe in the "return" statement Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-07-28 08:26 +0200
        Re: Pipe in the "return" statement Oliver Bestwalter <oliver@bestwalter.de> - 2011-07-25 16:35 +0200
          Re: Pipe in the "return" statement Archard Lias <archardlias@googlemail.com> - 2011-07-25 08:05 -0700
        Re: Pipe in the "return" statement John Gordon <gordon@panix.com> - 2011-07-25 14:39 +0000
          Re: Pipe in the "return" statement Archard Lias <archardlias@googlemail.com> - 2011-07-25 08:06 -0700
        Re: Pipe in the "return" statement gwowen <gwowen@gmail.com> - 2011-07-25 07:42 -0700
    Re: Pipe in the "return" statement Christian Heimes <lists@cheimes.de> - 2011-07-25 14:33 +0200
      Re: Pipe in the "return" statement Archard Lias <archardlias@googlemail.com> - 2011-07-25 08:28 -0700
        Re: Pipe in the "return" statement Christian Heimes <lists@cheimes.de> - 2011-07-25 18:11 +0200
          Re: Pipe in the "return" statement Archard Lias <archardlias@googlemail.com> - 2011-07-25 09:30 -0700
    Re: Pipe in the "return" statement TonyO <guinness.tony@gmail.com> - 2011-07-25 05:52 -0700
      Re: Pipe in the "return" statement gwowen <gwowen@gmail.com> - 2011-07-25 06:00 -0700
      Re: Pipe in the "return" statement red floyd <redfloyd@gmail.com> - 2011-07-25 14:04 -0700
        reading  zipfile; problem using raw buffer "Sells, Fred" <fred.sells@adventistcare.org> - 2011-07-26 08:42 -0400
          Re: reading  zipfile; problem using raw buffer Billy Mays <81282ed9a88799d21e77957df2d84bd6514d9af6@myhashismyemail.com> - 2011-07-26 08:49 -0400
        Re: reading  zipfile; problem using raw buffer Peter Otten <__peter__@web.de> - 2011-07-26 15:04 +0200
        RE: reading  zipfile; problem using raw buffer "Sells, Fred" <fred.sells@adventistcare.org> - 2011-07-26 11:22 -0400

Page 2 of 2 — ← Prev page 1 [2]


#10333 — Re: reading zipfile; problem using raw buffer

FromPeter Otten <__peter__@web.de>
Date2011-07-26 15:04 +0200
SubjectRe: reading zipfile; problem using raw buffer
Message-ID<mailman.1494.1311685503.1164.python-list@python.org>
In reply to#10311
Sells, Fred wrote:

> I'm tring to unzip a buffer that is uploaded to django/python.  I can
> unzip the file in batch mode just fine, but when I get the buffer I get
> a "BadZipfile exception.  I wrote this snippet to try to isolate the
> issue but I don't understand what's going on.  I'm guessing that I'm
> losing some header/trailer somewhere?

Why are you copying the file before you try to unzip it?

> def unittestZipfile(filename):
>     buffer = ''
>     f = open(filename)

Open the file in binary mode:

      f = open(filename, "rb")

>     for i in range(22):
>         block = f.read()
>         if len(block) == 0:
>             break
>         else:
>             buffer += block
>     print len(buffer)
>     tmp = open('tmp.zip', 'w')

Again, open the file in binary mode ("wb").

>     tmp.write(buffer)
>     tmp.close()

Not essential to the problem you are encountering, but you want 
shutil.copyfile(). Also, have a look at the shutil.copyfileobj() 
implementation to learn how to properly copy a file in chunks of limited 
size.

>     zf = zipfile.ZipFile('tmp.zip')
>     print dir(zf)
>     for name in zf.namelist():
>         print name
>         print zf.read(name)
> ____________________________________________________________
> 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)]
> Traceback (most recent call last):
>   File
> "C:\all\projects\AccMDS30Server\mds30\app\uploaders\xmitzipfile.py",
> line 162, in <module>
>     unittestZipfile('wk1live7.8to7.11.zip')
>   File
> "C:\all\projects\AccMDS30Server\mds30\app\uploaders\xmitzipfile.py",
> line 146, in unittestZipfile
>     print zf.read(name)
>   File "C:\alltools\python26\lib\zipfile.py", line 837, in read
>     return self.open(name, "r", pwd).read()
>   File "C:\alltools\python26\lib\zipfile.py", line 867, in open
>     raise BadZipfile, "Bad magic number for file header"
> zipfile.BadZipfile: Bad magic number for file header

[toc] | [prev] | [next] | [standalone]


#10337 — RE: reading zipfile; problem using raw buffer

From"Sells, Fred" <fred.sells@adventistcare.org>
Date2011-07-26 11:22 -0400
SubjectRE: reading zipfile; problem using raw buffer
Message-ID<mailman.1498.1311693743.1164.python-list@python.org>
In reply to#10311
Thanks all,  adding the 'rb' and 'wb' solved that test case.

The reason I read the file "the hard way" is that I'm testing why I
cannot unzip a buffer passed in a file upload using django.

While not actually using a file, pointing out the need for the binary
option gave me the clue I needed to upload the file

All is good and moving on to the next crisis ;)

Fred.

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.python


csiph-web