Error saving object from python script using GetParameterAsText

1558
7
Jump to solution
05-16-2021 12:26 PM
badrinathkar
New Contributor III

I have made pythan script tool in which the

workspace + "/Data.shp" is sucessfully saving the files as here:

>>> import arcpy
... import os
... import sys
... from arcpy import env
... arcpy.env.overwriteOutput = True
###########################################
... InFc = arcpy.GetParameterAsText(0)
... workspace= arcpy.GetParameterAsText(1)
... Output_Data_shp = workspace + "/Data.shp"
... Output_Table_dbf = workspace + "/Table.dbf"
... # Process: Table Select
... arcpy.TableSelect_analysis(InFc, Output_Table_dbf, "")
... # Process: Dissolve
... arcpy.Dissolve_management(InFc, Output_Data_shp, "", "", "MULTI_PART", "DISSOLVE_LINES")

But when I allow users to define the path, the tools fails to execute. For this I used following code:

>>> import arcpy
... import os
... import sys
... from arcpy import env
... arcpy.env.overwriteOutput = True
###########################################
... InFc = arcpy.GetParameterAsText(0)
... Output_Data_shp = arcpy.GetParameterAsText(1)
... Output_Table_dbf = arcpy.GetParameterAsText(2)
... # Process: Table Select
... arcpy.TableSelect_analysis(InFc, Output_Table_dbf, "")
... # Process: Dissolve
... arcpy.Dissolve_management(InFc, Output_Data_shp, "", "", "MULTI_PART", "DISSOLVE_LINES")

For information, I am running the Python script inside a normal toolbox (i.e. not inside a python toolbox). I have defined the data types correctly under the Parameters tab of the script. Although python is quite new to me and I am not trained enough to implement codes into the script to detect errors, I think the GetParametersAsText defines the directory with Single Backslash (/) which hinders the tool to execute.

Any suggestions to solve the issue? Thanks in advance.

 

0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

I prefer to let the os module do any path wrangling for me.

 

import os

Output_Data_shp = os.path.join(workspace, "Data.shp")
Output_Table_dbf = os.path.join(workspace, "Table.dbf")

View solution in original post

7 Replies
DanPatterson
MVP Esteemed Contributor

for the 2nd parameter (your workspace), do you have it defined as a "folder" ?

and did you try raw encoding

Output_Data_shp =  r"{}\\Data.shp".format(workspace)  # raw encoding 

 


... sort of retired...
badrinathkar
New Contributor III

My apologies. I had done a horrible mistake in posting the 2nd code (actually I posted the same code in both places).  Please consider the following:

workspace=  arcpy.GetParameterAsText(1)

workspace + "/Data.shp" 

workspace + "/Stat.dbf"  is working fine.

Tool fails to execute when I allow users to save the files using the GetParameterAsText:

Output_Data_shp = arcpy.GetParameterAsText(1)
Output_Table_dbf = arcpy.GetParameterAsText(2)

I have defined workspace as workspace data type.

0 Kudos
DanPatterson
MVP Esteemed Contributor

workspace .... should be defined as a "folder" in your toolbox.  Double backslashes as in my examples would be preferable (or use the os module as David suggested), you don't want to mix back and fore slashes.

 


... sort of retired...
0 Kudos
DavidPike
MVP Frequent Contributor

I prefer to let the os module do any path wrangling for me.

 

import os

Output_Data_shp = os.path.join(workspace, "Data.shp")
Output_Table_dbf = os.path.join(workspace, "Table.dbf")
badrinathkar
New Contributor III

My apologies. I had done a horrible mistake in posting the 2nd code (actually I posted the same code in both places).  Please consider the following:

workspace=  arcpy.GetParameterAsText(1)

workspace + "/Data.shp" 

workspace + "/Stat.dbf"  is working fine.

Tool fails to execute when I allow users to save the files using the GetParameterAsText:

Output_Data_shp = arcpy.GetParameterAsText(1)
Output_Table_dbf = arcpy.GetParameterAsText(2)

0 Kudos
badrinathkar
New Contributor III

My apologies. I had done a horrible mistake in posting the 2nd code (actually I posted the same code in both places).  Please consider the following:

workspace=  arcpy.GetParameterAsText(1)

workspace + "/Data.shp" 

workspace + "/Stat.dbf"  is working fine.

Tool fails to execute when I allow users to save the files using the GetParameterAsText:

Output_Data_shp = arcpy.GetParameterAsText(1)
Output_Table_dbf = arcpy.GetParameterAsText(2)

I have defined workspace as workspace data type.

0 Kudos
Luke_Pinner
MVP Regular Contributor

It would help if you posted the actual error message. But a wild stab in the dark is that you have an undeclared variable.. You declare the dbf as Output_Table_dbf  and then attempt to use Output_Stat_Table_dbf