Initially I was thinking of using os.path.isdir
but I don't think this works for zip files. Is there a way to peek into the zip file and verify that this directory exists? I would like to prevent using unzip -l "$@"
as much as possible, but if that's the only solution then I guess I have no choice.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
You can check for the directories with ZipFile.namelist().
for python(>=3.6):
this is how the
is_dir()
implemented in python source code:It simply checks if the filename ends with a slash
/
, Can't tell if this will work correctly in some certain circumstances(so IMO it is badly implemented).for python(<3.6):
as
print(zipinfo)
will showfilemode
but no corrsponding property or field is provided, I dive into zipfile module source code and found how it is implemented. (seedef __repr__(self):
https://github.com/python/cpython/blob/3.6/Lib/zipfile.py)possibly a bad idea but it will work:
if you want something simple and easy, this will work in most cases but it may fail because in some cases this field will not be printed.
Finally:
my solution is to check file mode manually and decide if the referenced file is actually a directory inspired by https://github.com/python/cpython/blob/3.6/Lib/zipfile.py line 391.
You can accomplish this using the built-in library ZipFile.
Tested and functional with Python32.
Just check the filename with "/" at the end of it.
You use this line
because it is possible that archive contains no directory explicitly; just a path with a directory name.
Execution result: