'Get top level directory within the zipfile
I have the following structure within a zipped file "test.zip"
JOU=00335/VOL=2019.30/ISU=9-10/ART=9812/data.xml
JOU=00335/VOL=2019.30/ISU=9-10/ART=9813/data.xml
JOU=00335/VOL=2019.30/ISU=9-10/ART=9814/data.xml
There are multiple top level folders like JOU=00336
,JOU=00337
etc.
I want to count the top level folders.
I have the following code which gives me a count of all XML files zipped in test.zip.
with ZipFile("test.zip", "r") as f:
print(len(f.namelist()))
Not sure how to count just the top level folders
Solution 1:[1]
You can split the paths to get the first element in them, and count those unique ones with a set
:
top = {item.split('/')[0] for item in f.namelist()}
print(len(top))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ofer Sadan |