【发布时间】:2014-03-12 01:36:22
【问题描述】:
目标是从 Internet 下载文件,并从中创建文件对象或类似文件的对象,而无需接触硬盘驱动器。这只是为了我的知识,想知道它是否可能或实用,特别是因为我想看看我是否可以避免编写文件删除行。
这就是我通常会从网络上下载东西并将其映射到内存的方式:
import requests
import mmap
u = requests.get("http://www.pythonchallenge.com/pc/def/channel.zip")
with open("channel.zip", "wb") as f: # I want to eliminate this, as this writes to disk
f.write(u.content)
with open("channel.zip", "r+b") as f: # and his as well, because it reads from disk
mm = mmap.mmap(f.fileno(), 0)
mm.seek(0)
print mm.readline()
mm.close() # question: if I do not include this, does this become a memory leak?
【问题讨论】:
标签: python python-requests mmap