I am trying to create a script tool to use in a model. The purpose of the script tool is to take files that have been exported to a folder and zip them into a different folder. I am getting an error:
Traceback (most recent call last):
File "C:\Scratch_Catalog\ZipFile_Export.py", line 33, in <module>
zip_folder(input_folder, output_zip)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Scratch_Catalog\ZipFile_Export.py", line 15, in zip_folder
with zipfile.ZipFile(output_zip, 'w', zipfile.ZIP_DEFLATED) as zipf:
~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\zipfile\__init__.py", line 1367, in __init__
self.fp = io.open(file, filemode)
~~~~~~~^^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied: 'C:\\Scratch_Catalog\\ZipTest'
I know that is a lot but even after research I am not understanding the error. Here is the attempted code
def zip_folder(input_folder, output_zip):
"""Compresses the contents of a folder into a ZIP file."""
if not os.path.exists(input_folder):
arcpy.AddError(f"Input folder does not exist: {input_folder}")
sys.exit(1)
# Ensure output directory exists
os.makedirs(os.path.dirname(output_zip), exist_ok=True)
with zipfile.ZipFile(output_zip, 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, _, files in os.walk(input_folder):
for file in files:
file_path = os.path.join(root, file)
arcname = os.path.relpath(file_path, input_folder)
zipf.write(file_path, arcname)
arcpy.AddMessage(f"Created ZIP file: {output_zip}")
"""Clear all cached workspaces"""
arcpy.management.ClearWorkspaceCache()
if __name__ == "__main__":
# Script tool parameters
input_folder = arcpy.GetParameterAsText(0) # Input folder
output_zip = arcpy.GetParameterAsText(1) # Output ZIP file path
zip_folder(input_folder, output_zip)
Any help is greatly appreciated.
Solved! Go to Solution.
The file filter just wants the extension without the period. So "zip", not ".zip"
Also, making sure that it's an output parameter.
Here's mine (attached)
I did get an error when I tried to zip a folder where there was an active lockfile on something inside btw.
That's odd. I copy-pasted your code exactly and it ran fine for me.
I'm assuming the error is on the output path, so what happens if you pick a different output folder?
I get the same error, seemingly no matter what I do with the input and output. I have tried changing the parameter values from folder to feature class, shapefile, or folder and mapping to them as well as changing the output folder location.
In every case I am getting the same error.
This is my setup
To confirm, your second parameter should be of "File" type (and then filtered to zips)
Are you able to do other work in that folder? Like an arcpy.management.CreateFeatureclass() Create Feature Class (Data Management)—ArcGIS Pro | Documentation
I definitely had the parameters setup wrong. I changed them to match yours. For the output, you filtered the file by ".zip"? I tried that and I can't even run the tool, I get the red X on the output parameter telling me it has to be a .zip file, which is confusing because the tool has not ran to create the .zip file.
I'm sure at this point this is operator error.
Thank you very much for your help. It is working now, the issue was I had it as ".zip". Thank you again!