【发布时间】:2019-02-09 16:14:23
【问题描述】:
我正在寻找一种方法来从包含多个文件的存档中提取特定文件(知道他的名字),而无需在硬盘驱动器上写入任何文件。
我尝试同时使用 StringIO 和 zipfile,但我只获得了整个存档,或者来自 Zipfile 的相同错误(打开需要另一个参数而不是 StringIo 对象)
需要的行为:
archive.zip #containing ex_file1.ext, ex_file2.ext, target.ext
extracted_file #the targeted unzipped file
archive.zip = getFileFromUrl("file_url")
extracted_file = extractFromArchive(archive.zip, target.ext)
到目前为止我已经尝试过:
import zipfile, requests
data = requests.get("file_url")
zfile = StringIO.StringIO(zipfile.ZipFile(data.content))
needed_file = zfile.open("Needed file name", "r").read()
【问题讨论】:
标签: python python-2.7