问题描述
我有一个 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 文件中的文件的文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!