Download zip file locally to tempfile, extract files to tempfile, and list the files

时间:2019-04-17 02:09:04

标签: python-3.x zipfile

I am trying to get back into python, and I am not sure where to start, but I want to download a zip file locally to temp, extract the files, and then list the files (which are 99.99% of the time csv files). I want to list the files so that I can further process one of the files based on logic with the benefit of the temp directory being removed automatically.

I can do most of above in R fairly succinctly, but my requirements are to map this idea to python and from my initial Google searches, the solutions that I am finding do not appear to be straightforward nor do exactly what I am looking for.

Any help would be greatly appreciated.

1 个答案:

答案 0 :(得分:1)

We start by downloading the file via requests

import requests

results = requests.get('<url_to_zip>')
with open('/tmp/zip_folder.zip', 'wb') as f:
    f.write(results.content)

We then extract the zip file to /tmp using zipfile

import zipfile
file = zipfile.ZipFile('/tmp/zip_folder.zip')
file.extractall(path='/tmp')

We then list the files.

files = os.listdir('/tmp/zip_folder')
for file in files:
    if 'csv' in file:
        print(file)