[GIS] How to make variables output to user defined workspace

arcgis-desktoparcpyparameterspythonworkspace

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")

Best Answer

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

Related Question