How to access a folder in a zip file?

dieter dieter at handshake.de
Mon Jan 21 02:04:25 EST 2019


mignoncharly via Python-list <python-list at python.org> writes:
> I'm a newbie in python and I was trying to access a folder inside a zip file and I also have issues with relative and absolute path. Thks in advance.
>
> In projects there a lot of urls (i'll read them later one by one and store them in a list/array so that I could work with it easily)
>
> here is the path:  home/name/tutorial/prof/ks.zip/projects
>
> should I use 
>
> file_name = "ks.zip" 

"file_name" should get the file path to your "zip" archive.
This path likely ends in "ks.zip" but likely has a prefix -- either to form
an "absolute" path (one starting with "/") or a relative path
starting from the "current working directory".
> ...
>  # opening the zip file in READ mode
> with ZipFile(file_name, 'r') as zip:

In the "with" block, "zip" is a "ZipFile Object".
Read its documentation in the "zipfile" module documentation.
Especially look at "namelist" (this give you the member names
of the archive) and "open" or "read".

The members of a folder in the "zip" archive will be charaterized
by a name prefix, ending in the folder name followed by "/".
There may (or may not) be a prefix before the folder name.




More information about the Python-list mailing list