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

python – 获取远程zip文件并列出其中的文件,第1张

概述我正在开发一个小型Google App Engine项目,我需要从URL中获取远程zip文件,然后列出zip存档中包含的文件. 我正在使用zipfile模块. 这是我到目前为止所提出的: # fetch the zip file from its remote URLresult = urlfetch.fetch(zip_url)# store the contents in a strea 我正在开发一个小型Google App Engine项目,我需要从URL中获取远程zip文件,然后列出zip存档中包含的文件.

我正在使用zipfile模块.

这是我到目前为止所提出的:

# fetch the zip file from its remote URLresult = urlfetch.fetch(zip_url)# store the contents in a streamfile_stream = StringIO.StringIO(result.content)# create the Zipfile objectzip_file = zipfile.Zipfile(file_stream,'w')# read the files by namearchive_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文件并列出其中的文件全部内容,希望文章能够帮你解决python – 获取远程zip文件并列出其中的文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1192524.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存