Select to view content in your preferred language

Unzip with python - Using ToolValidator

2174
7
Jump to solution
09-24-2012 11:04 AM
KarstenRank
Frequent Contributor
Hello,

I want to unzip a file to a directory, and show all the files from the zipped-file in the tool-parameters on self.params[2]. I think this should work with the ToolValidator.

So I write in the Validator the following script.
 def updateParameters(self):     """Modify the values and properties of parameters before internal     validation is performed.  This method is called whenever a parmater     has been changed."""     if self.params[0].value:       list = []       zip = zipfile.ZipFile(self.params[0].value, "r")       for zfile in zip.namelist():         list.append(zfile.name)       self.params[2].filter.list = list


My problem now is that I get the following error:

ERROR
updateParameters Execution Error: Runtime error : global name 'zipfile' is not defined


I have already imported zipfile in def __init__(self)

Can someone help me?

Thanks a lot for your help!!

Karsten
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JasonScheirer
Esri Alum
Try str(self.params[0].value) or self.params[0].value.value in the validator.

View solution in original post

0 Kudos
7 Replies
MathewCoyle
Honored Contributor
You will need to pass it as a parameter to the function or make it a global variable.
0 Kudos
JasonScheirer
Esri Alum
Did you import zipfile?
0 Kudos
KarstenRank
Frequent Contributor
I have import zipfile in the ToolValidator and in the python script.

class ToolValidator:
  """Class for validating a tool's parameter values and controlling
  the behavior of the tool's dialog."""

  def __init__(self):
    """Setup arcpy and the list of tool parameters."""
    import arcpy
    import zipfile
    
    self.params = arcpy.GetParameterInfo()

  def initializeParameters(self):
    """Refine the properties of a tool's parameters.  This method is
    called when the tool is opened."""
    return

  def updateParameters(self):
    """Modify the values and properties of parameters before internal
    validation is performed.  This method is called whenever a parmater
    has been changed."""
    if self.params[0].value:
      list = []
      zip = zipfile.ZipFile(self.params[0].value, "r")
      for zfile in zip.namelist():
        list.append(zfile.name)
      self.params[2].filter.list = list

  def updateMessages(self):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    return


But how can I pass it as a parameter to the function or make it a global variable?
0 Kudos
JasonScheirer
Esri Alum
Why not just put the import at the top of updateParameters? Python's smart about how it imports modules and if it's already imported it once it'll stay in memory and won't take any time at all if it hits another import line later for the same thing.

What version of ArcGIS are you using?
0 Kudos
KarstenRank
Frequent Contributor
I'm using ArcMap 10.

Now I get another error. The import at the beginning has helped loading the module.

ERROR
updateParameters Execution Error: Runtime error : ValueObject: Get attribute seek does not exist
0 Kudos
JasonScheirer
Esri Alum
Try str(self.params[0].value) or self.params[0].value.value in the validator.
0 Kudos
KarstenRank
Frequent Contributor
Thanks a lot!
It works now!!

Greetings from Bavaria!!

Karsten
0 Kudos