0

I am really new to python and trying to download a .zip file from the URL and extract it on my system using the following code:

# importing necessary modules
import requests, zipfile
from io import BytesIO
print('Downloading started')

#Defining the zip file URL
url = 'https://enfxfr.dol.gov/data_catalog/MSHA/msha_accident_20230422.csv.zip'

# Split URL to get the file name
filename = url.split('/')[-1]

# Downloading the file by sending the request to the URL
req = requests.get(url)
print('Downloading Completed')

# extracting the zip file contents
zipfile= zipfile.ZipFile(BytesIO(req.content))
zipfile.extractall('C:/Users/ssawant/OneDrive - Iron Senergy/Desktop/MSHA converted data/msha_accident')

but getting this error instead:

Traceback (most recent call last):
  File "c:\Users\ssawant\OneDrive - Iron Senergy\Desktop\MSHA converted data\test_2.py", line 17, in <module>
    zipfile= zipfile.ZipFile(BytesIO(req.content))
  File "C:\Users\ssawant\AppData\Local\Programs\Python\Python39\lib\zipfile.py", line 1266, in __init__
    self._RealGetContents()
  File "C:\Users\ssawant\AppData\Local\Programs\Python\Python39\lib\zipfile.py", line 1333, in _RealGetContents
    raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file

I was expecting CSV file extracted from the .zip file.

Michael Ruth
  • 2,938
  • 1
  • 20
  • 27
  • 2
    https://stackoverflow.com/questions/3083235/unzipping-file-results-in-badzipfile-file-is-not-a-zip-file – Captain Caveman May 02 '23 at 17:03
  • You'll find good existing answers by breaking this problem into two: 1. how to download a file from URL and 2. how to extract a zip file – Kache May 02 '23 at 17:25

0 Answers0