0

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")
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked May 6, 2014 at 0:45

2 Answers 2

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

answered May 6, 2014 at 1:22
2
  • 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. Commented 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? Commented May 6, 2014 at 21:24
0

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

  1. 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)
    
  2. 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.

answered May 8, 2014 at 15:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.