I'm working on a script that originally had locations coded in but I want to make it where anyone in my office can use it and they can set their workspace up however they want. I've almost got it to work how I want. I think I have one more obstacle then I'm good.
I have some parameters set up for user input.
arcpy.env.workspace = arcpy.GetParameterAsText(0)
FileGDB_name = arcpy.GetParameterAsText(1)
NewLocation = arcpy.CreateFileGDB_management(arcpy.env.workspace, FildGDB_Name)
clipLayer = arcpy.Clip_analysis(RouteLayer, StateLayer, NewLocation + name + "Clip")
RouteLayer and StateLayer are additional parameters set up by the user. The script fails when it gets to the clip tool. I realize the way I have it set up, NewLocation calls the CreateFileGDB tool rather than the location of the GDB. I want to be able to create the GDB and store it as a variable so it can be used in other tools in the script. Could someone shed some light on how to do this?
Solved! Go to Solution.
ArcGIS geoprocessing tools return Result—ArcPy classes | ArcGIS Desktop objects. You can't append a string to a result object the way you are trying in your code. Try changing the NewLocation line to:
NewLocation = arcpy.CreateFileGDB_management(arcpy.env.workspace, FildGDB_Name).getOutput(0)
I just tried that. The script will run but the results don't have any records. Is there another way to create the GDB and store as a variable? Maybe I'm not understanding the whole .getOutput(0) thing. Sorry, it's been a while since I've done any python. I did some in college 4 years ago but never thought I would actually be using it.
I think it's the way you worded the output, try this for your next line.
clipLayer = arcpy.Clip_analysis(RouteLayer, StateLayer, NewLocation + "/Clip")
If you want it as a variable.
GDBvar = arcpy.env.workspace + "\\" + FileGDB_name
That still gives me a result with no records.
I want to keep the name variable in there. That is holding the name of the state for each clip layer (i.e. name = "Texas"). For example, an output would be something like .......New.gdb\TexasClip which is why I thought NewLocation + name + "Clip" would work.
So here's the code I have. I just ran like this. It'll create the GDB but the result of the clip tool gets stored at the workspace level rather than being stored in the GDB. I must have been using some bad data earlier which may have been causing the output to have no records. The output now has records. However, I want a feature class stored inside the GDB rather than a shapefile.
arcpy.env.workspace = arcpy.GetParameterAsText(0) RouteLayer = arcpy.GetParameterAsText(1) StateLayer = arcpy.GetParameterAsText(2) StateTaxDistricts = arcpy.GetParameterAsText(3) FileGDB_Name = arcpy.GetParameterAsText(4) GDB = arcpy.CreateFileGDB_management(arcpy.env.workspace, FileGDB_Name).getOutput(0) stateName = os.path.basename(StateLayer).rstrip(os.path.splitext(StateLayer)) name = stateName[:-1] clipLayer = arcpy.Clip_analysis(RouteLayer, StateLayer, GDB + name + "_Clip")