0

I would like t write the script where all zip files in the folder would be extracted in another folder. I found a helpful answer here: Unzip all zipped files in a folder to that same folder using Python 2.7.5

I slightly edited the code from the answers there as listed down below

import zipfile36 as zipfile

working_dir = r"C:\Users\Tim\Desktop\Tim\Faks\NMG\Python\Python Scripting for Geoprocessing Workflows\PythonGP\PythonGP\Scripts" ###### LAHKO MENJAŠ POT ######
goal_dir= r"C:\Users\Tim\Desktop\Tim\Faks\NMG\Python\Python Scripting for Geoprocessing Workflows\PythonGP\PythonGP\Scripts\Ekstrahirano"
extension = ".zip"

for item in os.listdir(working_dir): # loop through items in dir
    if item.endswith(extension): # check for ".zip" extension
        file_name = os.path.abspath(item) # get full path of files
        zip_ref = zipfile.ZipFile(file_name) # create zipfile object
        zip_ref.extractall(goal_dir) # extract file to dir
        zip_ref.close() # close file
        os.remove(file_name) # delete zipped file

When running, I am constantly receiving this error

Traceback (most recent call last):
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\IPython\core\interactiveshell.py", line 3437, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-98-d46cd220b77f>", line 8, in <module>
    zip_ref = zipfile.ZipFile(file_name) # create zipfile object
  File "C:\Users\Tim\AppData\Roaming\Python\Python39\site-packages\zipfile36.py", line 1100, in __init__
    self._RealGetContents()
  File "C:\Users\Tim\AppData\Roaming\Python\Python39\site-packages\zipfile36.py", line 1167, in _RealGetContents
    raise BadZipFile("File is not a zip file")
zipfile36.BadZipFile: File is not a zip file

What am I missing?

Timko99
  • 21
  • 3
  • Welcome to SO! Have you checked https://stackoverflow.com/q/3083235/12696223? – Momo Oct 09 '22 at 20:57
  • maybe first display filename and check if you can open this file with other programs. Maybe file is broken and you should skip it. – furas Oct 09 '22 at 23:40
  • why do you use `zipfile36` ? Why not standard `zipfile`? Even [documentation](https://pypi.org/project/zipfile36/) suggests to use standard `zipfile` for Python newer than 3.6 - and you use Python 3.9 – furas Oct 09 '22 at 23:43
  • Thanks Furas. I tried to open one of the zip files that are downloaded prior to this code and it is "either in unknown format or damaged". I do not know why, but for sure cannot be extracted. The code downloads those zip files using beautifulsoup and requests. – Timko99 Oct 10 '22 at 20:55

0 Answers0