簡體   English   中英

獲取遠程zip文件並列出其中的文件

[英]Fetch a remote zip file and list the files within

我正在開發一個小型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

你正在打開ZipFile進行寫作。 請嘗試閱讀。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM