我正在开展一个小型 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/