[英]How to list a folder's files using the zipfile module in Python
I'm having a .zip file which contains lots of files and directories in it. 我有一个.zip文件,其中包含很多文件和目录。 Its structure resembles the following sketch.
其结构类似于以下草图。
/
contents/
--file1.txt
--file2.txt
lists/
--file3.txt
--file4.txt
file5.txt
file6.txt
I am currently interested on listing (and reading) the files residing on the particular subfolder called contents
. 我对上市(和阅读)驻留在被称为特定的子文件夹中的文件目前感兴趣的
contents
。
However I can't seem to find a useful function to do that since the zipfile module's namelist
function will just list every signle file. 但是我似乎找不到一个有用的函数来执行此操作,因为zipfile模块的
namelist
函数将仅列出每个signle文件。 The only other way I can think of is to extract everything on a temp folder read what I need to and then delete it. 我能想到的唯一另一种方法是提取临时文件夹中的所有内容,以读取需要的内容,然后将其删除。 But I consider it a dumb approach.
但我认为这是一种愚蠢的做法。
Any other ideas? 还有其他想法吗?
Thanks in advance. 提前致谢。
过滤它。
dirfiles = [f for f in z.namelist() if f.startswith(pathprefix)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.