Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know an item is a directory while looping over zipfile in Python?

Doing something like this:

from zipfile import ZipFile

#open zip file
zipfile = ZipFile('Photo.zip')

#iterate zip contents
for zipinfo in zipfile.filelist:
    #do something
    filepath, filename = path.split(zipinfo.filename)

How do I know if zipinfo is a file or a directory?

Thanks for your support.

like image 321
neurino Avatar asked Jan 19 '12 22:01

neurino


People also ask

How do you check if it is a zip file in Python?

isdir() or a file with TarInfo. isfile() . Similarly you can determine whether a file is a zip file using zipfile. is_zipfile() .

What is ZipFile in Python?

The ZIP file format is a common archive and compression standard. This module provides tools to create, read, write, append, and list a ZIP file. Any advanced use of this module will require an understanding of the format, as defined in PKZIP Application Note.


1 Answers

Probably this is the right way:

is_dir = lambda zipinfo: zipinfo.filename.endswith('/')
like image 156
neurino Avatar answered Oct 31 '22 18:10

neurino