加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 11:18:30 所属栏目:Python 来源:网络整理
导读:我正在开发一个小型Google App Engine项目,我需要从URL中获取远程zip文件,然后列出zip存档中包含的文件. 我正在使用zipfile模块. 这是我到目前为止所提出的: # fetch the zip file from its remote URLresult = urlfetch.fetch(zip_url)# store the content
我正在开发一个小型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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读