简体   繁体   中英

How does one use ZipInfo in Python?

Could someone please explain how exactly ZipInfo is supposed to be used? It says that ZipInfo.comment can access "comment for the individual archive member" I didn't even know archive members can have comments %\\ ...

I tried getting it with:

data = zipfile.ZipFile('filename')
info = data.infolist()

but what I'm getting looks like:

   [<zipfile.ZipInfo object at 0x0257DBF8>, <zipfile.ZipInfo object at 0x026A7030>, <zipfile.ZipInfo object at 0x026A7098>, ... ]

I don't know what that means :(

Also, i can't seem to call zipinfo.comment at all, but from above it looks like infolist() is the same thing?

So confused...

Calling data.infolist() is giving you a list of ZipInfo objects. These are descriptions of all the individual files and directories stored inside your zip archive (and not the files/directories themselves). To manipulate these individual files/directories, you have to call a method of your ZipFile object data with the name from info . For example if you want to print the first 10 characters in each file you could run

for f in info:
    data.read(f)[:10]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM