Downloading and unzipping a .zip file without writing to disk

前端 未结 9 1586
囚心锁ツ
囚心锁ツ 2020-12-02 04:42

I have managed to get my first python script to work which downloads a list of .ZIP files from a URL and then proceeds to extract the ZIP files and writes them to disk.

9条回答
  •  醉话见心
    2020-12-02 05:20

    Adding on to the other answers using requests:

     # download from web
    
     import requests
     url = 'http://mlg.ucd.ie/files/datasets/bbc.zip'
     content = requests.get(url)
    
     # unzip the content
     from io import BytesIO
     from zipfile import ZipFile
     f = ZipFile(BytesIO(content.content))
     print(f.namelist())
    
     # outputs ['bbc.classes', 'bbc.docs', 'bbc.mtx', 'bbc.terms']
    

    Use help(f) to get more functions details for e.g. extractall() which extracts the contents in zip file which later can be used with with open.

提交回复
热议问题