Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Zipfile:获取Zipfile中的顶级目录_Python_Python 3.x_Zipfile - Fatal编程技术网

Python Zipfile:获取Zipfile中的顶级目录

Python Zipfile:获取Zipfile中的顶级目录,python,python-3.x,zipfile,Python,Python 3.x,Zipfile,我在压缩文件“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 有多个顶级文件夹,如JOU=00336,JOU=00337等 我想数一数顶级文件夹 下面的代码为我提供了test.zip中压缩的所有XML文件的计数

我在压缩文件“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
有多个顶级文件夹,如
JOU=00336
JOU=00337

我想数一数顶级文件夹

下面的代码为我提供了test.zip中压缩的所有XML文件的计数

        with ZipFile("test.zip", "r") as f:
            print(len(f.namelist()))

不知道如何只计算顶级文件夹的数量

您可以拆分路径以获取其中的第一个元素,并使用
集对那些唯一的文件夹进行计数

top = {item.split('/')[0] for item in f.namelist()}
print(len(top))