[Tutor] identifying if a path is heading into a zip file - windows

Cameron Simpson cs at cskk.id.au
Sun Mar 20 17:54:16 EDT 2022


On 20Mar2022 02:13, nathan tech <nathan-tech at hotmail.com> wrote:
>Take a path, for instance:
>
>path="c:\my folder"
>
> os.path.isdir(path) = True
>
>Take a second path:
>
>path= 'c:\my folder\a file.zip\a folder in the zip'
>
>Is there a way for python to know, without me physically breaking it 
>down, that that path is in a zip file?
>
>For instance, a way for it to know it's dealing with a zip archive 
>file, and not just a folder with .zip in the name?

Well, I would start by just `os.stat(path)`. Since a path entering a zip 
file is meaningless, because to the OS it the zip is just a file, and 
therefore has no children) the stat should fail.

So if the stat succeeds, you know the "a file.zip" is in fact a folder.

If the stat fails, _then_ you need to exampine the path components 
individually with os.path.isfile. And once you hi a file-ending-in-.zip, 
maybe further examine it with the `zip` module to see if it really is a 
zip file. And if it contains "a folder in the zip".

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list