I am running a script in ArcMap and I want to reference the user defined workspace as an output instead of hard coding it in.
I have the scratch output set as a user defined parameter. However, I cannot figure out how to reference that when declaring the variable.
Below is a section of my code:
import arcpy
from arcpy.sa import*
import sys
from arcpy.mapping import*
#Extension
arcpy.CheckOutExtension("3D")
arcpy.CheckOutExtension("spatial")
arcpy.OverwriteOutput = True
#Argument
arcpy.env.scratchWorkspace = arcpy.GetParameterAsText(0)
#Variable
Survey_Points = "E:\\URSP\\Final\\Output.gdb\\Survey_Points"
#Process
arcpy.MakeXYEventLayer_management(Survey_Data, Survey_X, Survey_Y, "Survey_Layer", spRef, Survey_Z)
arcpy.CopyFeatures_management("Survey_Layer", Survey_Points, "", "0", "0", "0")
2 Answers 2
I think this is what you're asking.
You have a tool written in python and want to supply the output workspace on the command line or as a parameter as a tool. First you tell the tool to expect something:
enter image description here
and to pick it up in python use sys.argv[], the first augment is sys.argv[1]
, the second is sys.argv[2]
and so on. sys.argv[0]
is the full path to execute the script file, sometimes useful.
so your output workspace:
import sys, arcpy
from arcpy import env
env.workspace = sys.argv[1]
or you can set a variable output = sys.argv[1]
and use it like OutShp = output + "\\OutFile.shp"
or OutShp = "%s\\OutFile.shp" % output
-
This is user29866 that asked the original question. I'm not sure if I understand. Basically what I am asking is that I have set my workspace and scratch workspace as a parameter that is set for the user to input in the tool interface. However, I have all variables hard coded with a file path and I want to use the parameter instead of the file path. That way I can use the tool with different workspaces with out having to rewrite code.user29866– user298662014年05月06日 14:19:30 +00:00Commented May 6, 2014 at 14:19
-
I see, you're using GetParamaterAsText instead of sys.argv. So you wan to change the line Survey_Points = "E:\\URSP\\Final\\Output.gdb\\Survey_Points" to Survey_Points = arcpy.env.workspace + "\\Survey_Points" for the survey_points to end up where you specify... is that right?Michael Stimson– Michael Stimson2014年05月06日 21:24:01 +00:00Commented May 6, 2014 at 21:24
I believe that you are already getting your arcpy.env.scratchWorkspace
from tool parameter and you want to set the same path with feature class name appended to it as the output of arcpy.CopyFeatures_management
but you do not want to add any other parameter to your tool. So to achieve that, you have two quick ways
Keep every output feature class with unique name.
Replace:
Survey_Points = "E:\\URSP\\Final\\Output.gdb\\Survey_Points"
With:
# to create unique output name if the feature class # with same name present at the given workspace # Example: if I have "E:\\Data.gdb\\Survey_Points" then # new feature class generated will be "E:\\Data.gdb\\Survey_Points0" Survey_Points = arcpy.CreateUniqueName("Survey_Points", arcpy.env.scratchWorkspace)
Keep only one output feature class with predefined name
you can join the path with os module. To do this, add the os import line in your script as you already have
arcpy.OverwriteOutput = True
from os import path
and
Replace:
Survey_Points = "E:\\URSP\\Final\\Output.gdb\\Survey_Points"
With:
# To join path of workspace with feature class name. # Output feature class with same name # will over write any previous feature class # Exaple: if you have "E:\\Data.gdb\\Survey_Points" # will be replaced with new feature class after one run Survey_Points = path.join(arcpy.env.scratchWorkspace, "Survey_Points")
After this, you do not have to change your code if someone changes the workspace.
Explore related questions
See similar questions with these tags.