Creating script tool from python - cant get parameter/data type to work correctly.

1215
4
07-01-2013 01:20 PM
ChristiNelson1
Occasional Contributor
Hi all,

My script runs fine in PythonWin and in the ArcGIS Python Window, so the code is good.  But for some reason I can't figure out how to set the parameters in the Add Script Wizard (ArcGIS 10.0 SP 5).  My script essentially creates data driven pages using a parcel as the index grid.  Each map is then exported to a pdf.  In my script, the output path (outpath) and the output folder (outfolder) are combined in the CreateFolder_management dialogue (to create the output folder if it does not exist).

My outpath is "C:\\" and my outfolder is "temp\\" to create "C:\\temp\\" when joined together.

In the Add Script Wizard I have defined the Display Names and Data Types as follows:

Display Name:                                 Display Type:
Output PDF file path:                          Folder             (this is not allowing me to browse to just "C:\\")
Output PDF file folder:                        Folder            

My question is: what Display Type do I need to set for the Output PDF file path, so that it will take an input of only C:\\ (and not some subfolder under C:\\)?


A snippet of my code is below and an attachment image of my Add Script Wizard is attached.
 #Check for existance of the output data before running the export tool.

                #Export to PDF
                outpath = "C:\\" #arcpy.GetParameterAsText(2)
                outfolder = "temp\\" #arcpy.GetParameterAsText(3)
                pdfLocation =  os.path.join(outpath, outfolder) #"C:\\temp\\"

                #print "pdfLocation:  " + pdfLocation
               
                pdfName = pdfLocation + APN + '.pdf'

                #print "pdfName:  " + pdfName

                if arcpy.Exists(pdfLocation):
                        pass
                        print "Folder Exists"
                else:
                        print r"Folder does not exist. Creating Folder"
                        arcpy.CreateFolder_management(outpath, outfolder)
                       
                if arcpy.Exists(pdfName):
                    arcpy.Delete_management(pdfName)

               CODE]

Thanks,
Christi
Tags (2)
0 Kudos
4 Replies
StacyRendall1
Occasional Contributor III
Hi Christi,

I noticed in the picture you included that your output paths have the direction Output. Although they specify where you want your output files to go, they are in fact inputs to your script, so they should be listed as Input. Outputs are things that your script tool gives you back, for example a script that manipulates a feature class might output the feature class so that it could then be used by another tool in ModelBuilder. In the case of your script there are no outputs.

The next thing you need to do is test that your inputs are not getting messed up somehow. To do this you can use arcpy.AddMessage() to print to the results dialog, like so:
#Export to PDF
outpath = "C:\\" #arcpy.GetParameterAsText(2)
arcpy.AddMessage('Outpath: ' + outpath)
outfolder = "temp\\" #arcpy.GetParameterAsText(3)
arcpy.AddMessage('Outfolder: ' + outfolder)
pdfLocation = os.path.join(outpath, outfolder) #"C:\\temp\\"
arcpy.AddMessage('pdfLocation: ' + pdfLocation)

Now when you run your script tool it will print all the paths to the screen so you can check that bits didn't go missing or get mangled...

If neither of the above helps, you can try changing the Data Type of these two variables from Folder to String. This might help, but will mean that Arc won't enforce its type. So the user will have to correctly enter the paths. If they type in 'C:\' rather than 'C:\\' or 'C:' or 'C:/' it will cause a problem, as \ is a special character in Python.
0 Kudos
ChristiNelson1
Occasional Contributor
Hi,

I changed the output paths to have the direction *Input* instead of Output.  This was a successful change.  I added the arcpy.AddMessage() to print to the results dialog and it shows that everything is looping thru and exported.  However, when I go to the output location, no pdf files are there - nothing has been exported!

Is it a slash syntax problem? Although they are marked as Folder data types, it seems that the real value for the path is not being created. The path still shows backslashes (C:\) rather than C:\\ (see attachment). If this is the case, this would be a major problem in general when creating a tool.

How should I update my code to make sure the correct paths are maintained? In other words, how do I convert the paths entered as arcpy.GetParameterAsText to Python rawstrings using the "r directive", or is there a better way?
0 Kudos
ChristiNelson1
Occasional Contributor
Okay, I got it!

First I had to adjust the Data Types in the Script Properties (see attachment).  The Output PDF file path now has a Data Type: Workspace.  The Output PDF file folde now has a Data Type: Folder.

Next, I had to add another arcpy.AddMessage() for pdfName, to see what was happening when I ran the script.  The results displayed that I had to add in another "\" in pdfName, but in python it has to be "\\".  See code below:

               #Check for existance of the output data before running the export tool.
                #Export to PDF
                outpath = arcpy.GetParameterAsText(2) #"C:\\"
                arcpy.AddMessage('Outpath: ' + outpath)
                outfolder = arcpy.GetParameterAsText(3)
                arcpy.AddMessage('Outfolder: ' + outfolder) 
                pdfLocation = os.path.join(outpath, outfolder) #"C:\\temp\\"

                #print "pdfLocation:  " + pdfLocation
                arcpy.AddMessage('pdfLocation: ' + pdfLocation)
                
                pdfName = pdfLocation + "\\" + APN + '.pdf'
                arcpy.AddMessage('pdfName: ' + pdfName)
                #print "pdfName:  " + pdfName

                if arcpy.Exists(pdfLocation):
                        pass
                        #print "Folder Exists"
                        arcpy.AddMessage('Folder Exists') 
                else:
                        #print r"Folder does not exist. Creating Folder"
                        arcpy.AddMessage("Folder does not exist. Creating Folder...") 
                        arcpy.CreateFolder_management(outpath, outfolder)
                        
                if arcpy.Exists(pdfName):
                    arcpy.Delete_management(pdfName)

                try:
                    arcpy.mapping.ExportToPDF(mxDoc, pdfName)
                    arcpy.AddMessage("Export complete")
                except:
                    arcpy.AddError(arcpy.GetMessages(2))

##                if recordCounter > 2:
##                        break
                recordCounter +=1
                
        #print str(recordCounter) + " Records Exported"
        arcpy.AddMessage(str(recordCounter) + " Records Exported")
        del row, rows
        del i


Thanks so much for your help!
Christi
0 Kudos
StacyRendall1
Occasional Contributor III
Good work! Yes, AddMessage is very handy for tracking down stuff like that!
0 Kudos