获取包含在 zip 文件中的文件的文件列表 [英] get file list of files contained in a zip file

查看:52
本文介绍了获取包含在 zip 文件中的文件的文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 zip 存档:my_zip.zip.里面是一个txt文件,我不知道它的名字.我正在看Python的zipfile模块(http://docs.python.org/library/zipfile.html ),但对我正在尝试做的事情没有太大意义.

我将如何执行相当于双击"zip 文件以获取 txt 文件然后使用 txt 文件的操作:

<预><代码>>>>f = open('my_txt_file.txt','r')>>>内容 = f.read()

解决方案

你需要的是 ZipFile.namelist() ,它会给你一个包含所有存档内容的列表,然后你就可以执行 zip.open('filename_you_discover') 以获取该文件的内容.

I have a zip archive: my_zip.zip. Inside it is one txt file, the name of which I do not know. I was taking a look at Python's zipfile module ( http://docs.python.org/library/zipfile.html ), but couldn't make too much sense of what I'm trying to do.

How would I do the equivalent of 'double-clicking' the zip file to get the txt file and then use the txt file so I can do:

>>> f = open('my_txt_file.txt','r')
>>> contents = f.read()

解决方案

What you need is ZipFile.namelist() that will give you a list of all the contents of the archive, you can then do a zip.open('filename_you_discover') to get the contents of that file.

这篇关于获取包含在 zip 文件中的文件的文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆