[Tutor] zip question

Max Noel maxnoel_fr at yahoo.fr
Tue May 10 02:45:21 CEST 2005


On May 10, 2005, at 01:18, D. Hartley wrote:
> I admitted that my grasp of classes (and "constructors") is a bit
> fuzzy. I did get this particular class to work, and got the first half
> of the problem done.  However, now i'm working in another class,
> zipinfo.  It says:
>
> Instances of the ZipInfo class are returned by the getinfo() and
> infolist() methods of ZipFile objects. Each object stores information
> about a single member of the ZIP archive.
>
> Instances have the following attributes:
>
> filename, comment, etc.
>
>
> I've tried about ten things to get "filename" to work:
>
> myzip.filename("99905.txt") (str not callable)

     ZipFile.filename is an attribute, not a method. It contains the  
name of the archive (in other words, in your case myzip.filename ==  
"myzip.zip").

> myzip.comment(file("99905.txt")) (no file with this name - i suppose,
> if it's still zipped?)

     What you're doing in that line is open a file for reading, named  
99905.txt and located in the current folder, and *then* passing it to  
the ZipFile.comment method. Obviously not working. :D

> myzip.getinfo(99905.txt) (invalid syntax)

     Aah! So close! The quotes, dear, you forgot about the quotes! ;)

 >>> myzip.getinfo("99905.txt")
<zipfile.ZipInfo instance at 0x5d8a0>

     (cue Final Fantasy VII victory fanfare)

     Also, you may want to have a look at the ZipFile.NameToInfo  
attribute. In your case, myzip.NameToInfo is a dictionary which  
contains ZipInfo's for each of the files contained in the archive,  
where the key for each ZipInfo is the name of the corresponding  
files. In other words,

 >>> myzip.NameToInfo["99905.txt"]
<zipfile.ZipInfo instance at 0x5d8a0>

     Yeah, used that way it pretty much does the same thing as getinfo 
(), but I think it's more Pythonic (using a getinfo method feels Java- 
ish).


Hope that helps,
-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting  
and sweating as you run through my corridors... How can you challenge  
a perfect, immortal machine?"



More information about the Tutor mailing list