[英]List all files inside a folder in a zip file in python
I have a zip file structure like - B.zip/org/note.txt I want to directly list the files inside org folder without going to other folders in B.zip我有一个 zip 文件结构,例如 - B.zip/org/note.txt 我想直接列出 org 文件夹中的文件,而不转到 B.zip 中的其他文件夹
I have written the following code but it is listing all the files and directories available inside the B.zip file我编写了以下代码,但它列出了 B.zip 文件中可用的所有文件和目录
f = zipfile.ZipFile('D:\python\B.jar')
for name in f.namelist():
print '%s: %r' % (name, f.read(name))
You can filter the yields by startwith function.(Using Python 3) 您可以通过startwith函数过滤产量。(使用Python 3)
import os
import zipfile
with zipfile.ZipFile('D:\python\B.jar') as z:
for filename in z.namelist():
if filename.startswith("org"):
print(filename)
Everytime I came into this post making a similar question... But different at the same time.
每次我进入这篇文章时都会提出类似的问题......但同时又有所不同。 Cause of this, I think other users can have the same doubt.
因此,我认为其他用户可能会有同样的疑问。 If you got to this post trying this....
如果你到这篇文章尝试这个......
import os
import zipfile
# Use your folder path
path = r'set_yoor_path'
for file in os.listdir(os.chdir(path)):
if file[-3:].upper() == 'ZIP':
for item in zipfile.ZipFile(file).namelist():
print(item)
If someone feels that this post has to be deleted, please let m know.如果有人觉得这篇文章必须删除,请告诉我。 Tks
Tks
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.