Python - 使用请求直接下载文件到内存 [英] Python - Download File Using Requests, Directly to Memory

查看:36
本文介绍了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆