Python - Download File Using Requests, Directly to Memory

后端 未结 3 421
孤城傲影
孤城傲影 2020-12-03 05:16

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 kn

3条回答
  •  一个人的身影
    2020-12-03 05:41

    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)
    

提交回复
热议问题