目标是从互联网下载文件,并从中创建一个文件对象或类似文件的对象,而不必将其接触硬盘。这只是为了我的知识,想知道是否可能或实用,特别是因为我想看看是否可以规避编写文件删除行的代码。
这是我通常从网络上下载东西并映射到内存的方法:
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?