python - 获取远程 zip 文件并列出其中的文件

标签 python google-app-engine zip

我正在开展一个小型 Google App Engine 项目,我需要从 URL 获取远程 zip 文件,然后列出 zip 存档中包含的文件。

我正在使用 zipfile 模块。

这是我到目前为止的想法:

# fetch the zip file from its remote URL
result = urlfetch.fetch(zip_url)

# store the contents in a stream
file_stream = StringIO.StringIO(result.content)

# create the ZipFile object
zip_file = zipfile.ZipFile(file_stream, 'w')

# read the files by name
archive_files = zip_file.namelist()

不幸的是,archive_files 列表的长度始终为 0。

知道我做错了什么吗?

最佳答案

您正在使用 w 权限打开文件,这会截断它。将其更改为 r 阅读权限:

zip_file = zipfile.ZipFile(file_stream, 'r')

引用:http://docs.python.org/library/zipfile.html#zipfile-objects

关于python - 获取远程 zip 文件并列出其中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5770480/

相关文章:

google-app-engine - GWT:解密 GAE-JDO key 以访问父级

python - 如何用 Python 解析一些错误的 JSON?

linux - 有没有办法检查 zip 文件是否受密码保护?

python - Django 模型 : Get elements considering their presence as a foreign key in another table

python - 慢 python 代码似乎适合 itertools : how to optimize?

Python 方法 dir() 不返回所有属性/方法

google-app-engine - Google PubSub getMessageID = null 因为标签错误?

python - Epoch 1/2 103/Unknown - 8s 80ms/step - loss : 0. 0175(model.fit() 即使在超过训练图像总数后也一直运行)

windows - 解压缩大型 zst 文件

java - 如何在不创建任何新文件的情况下访问 zip 文件中目录中的文件?