Python-使用请求将文件直接下载到内存 [英] Python - Download File Using Requests, Directly to Memory
本文介绍了Python-使用请求将文件直接下载到内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目标是从Internet下载文件,并从中创建文件对象或类似文件的文件,而无需使其接触硬盘驱动器.这仅是出于我的知识,想知道它是否可能或可行,尤其是因为我想看看是否可以绕过必须编写文件删除行的代码.
The goal is to download a file from the internet, and create from it a file object, or a file like object without ever having it touch the hard drive. This is just for my knowledge, wanting to know if its possible or practical, particularly because I would like to see if I can circumvent having to code a file deletion line.
这通常是我从网络上下载内容并将其映射到内存的方式:
This is how I would normally download something from the web, and map it to memory:
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?
推荐答案
这就是我最终要做的事情.
This is what I ended up doing.
import zipfile
import requests
import StringIO
u = requests.get("http://www.pythonchallenge.com/pc/def/channel.zip")
f = StringIO.StringIO()
f.write(u.content)
def extract_zip(input_zip):
input_zip = zipfile.ZipFile(input_zip)
return {i: input_zip.read(i) for i in input_zip.namelist()}
extracted = extract_zip(f)
这篇关于Python-使用请求将文件直接下载到内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文