Having issues with arcpy.env.workspace.

3697
5
Jump to solution
10-21-2020 09:57 PM
AndrewSmith
New Contributor III

I have set my workspace, but when I run a simple clip. It just makes a shapefile in the folder location not the geodatabase I have set. The geodatabase is created in the location the user specifies. But it is dumping it into the folder not the geodatabase. 

I am working pin Python 2.7, and ArcMap 10.6 

here is my code. 

# Seting up folder path parameters from ArcGIs Tool Box
FolderPath = arcpy.GetParameterAsText(4)

# Naming the Geodatabase for the user.
WorkGDB = "MTAP_Data"

tempdata = os.path.join(FolderPath, WorkGDB)

# Ensuring if rerun that the output can be overwritten
arcpy.env.overwriteOutput = True

# make sure gdb does not already exist
if arcpy.Exists(tempdata):
arcpy.env.workspace = tempdata
else:
# Create the new file geodatabase and or overwrite if there is one in place already
arcpy.CreateFileGDB_management(FolderPath, WorkGDB)

# Set the working environment for Project

arcpy.env.workspace = tempdata

# set up outputwork space outWS
outWS = tempdata

# Send Arcpy message to verify workspace has been set.
arcpy.AddMessage("Workspace has been set to: " + str(arcpy.env.workspace))

# Message for tool box outputs - shows location of folder to user
arcpy.AddMessage("Geodatabase has been created here:" + FolderPath)

# ----------------- End of Geodatabase and folder creation --------------------#


# -------------Gather Global base data and Clip to user areas -----------------#
## Issue with workspace - it is not saving into the workspace, as well as it is not saving the right name.
# User defined input for clipping the regions. (can be Operating areas, or one Op Area
# clipInput = arcpy.GetParameterAsText(1)
clipInput = r'\\scripts\Data\Data.gdb\AOI'

OLD = r'Database Connections\my.sde\'OLDData'

arcpy.Clip_analysis(OLD,clipInput,outWS)

0 Kudos
1 Solution

Accepted Solutions
JoeBorgione
MVP Emeritus

Check the syntax of Clip:  it appears that your clipOutput variable is set to a geodatabase and not a feature class.

That should just about do it....

View solution in original post

5 Replies
JoshuaBixby
MVP Esteemed Contributor

Not sure this is related, but what are you expecting this string be, i.e., print out as?

OLD = r'Database Connections\my.sde\'OLDData'

0 Kudos
AndrewSmith
New Contributor III

I have cleaned it up a bit last night to work when I put the direct path for the output but when I switch it over to the variable it no longers works. 

import os, arcpy, time


#------------------- Set up File Geodatabase for Project work -----------------#

# Setting up folder path parameters from ArcGIs Tool Box
FolderPath = arcpy.GetParameterAsText(0)

# Naming the Geodatabase foe the user.
WorkGDB = "MTAP_Data.gdb"

# Ensuring if rerun that the output can be over written
arcpy.env.overwriteOutput = True

# Create the new file geodatabase and or over write if there is one in place already
arcpy.CreateFileGDB_management(FolderPath, WorkGDB)

# Set the working environment for MTAP Project
tempdata = os.path.join(FolderPath, WorkGDB)
arcpy.env.workspace = tempdata

# Send Arcpy message to verify workspace has been set.
arcpy.AddMessage("Workspace has been set to: " + str(arcpy.env.workspace))

# Message for tool box outputs - shows location of folder to user
arcpy.AddMessage("Geodatabase has been created here:" + FolderPath)

# ----------------- End of Geodatabase and folder creation --------------------#

# -------------Gather Global base data and Clip to user areas -----------------#

# User defined input for clipping the regions. (can be Operating areas, or one Op Area
# clipInput = arcpy.GetParameterAsText(1)
clipInput = r'\\Data\Data.gdb\AOI'

OLD = r'\\Data\Data.gdb\AREA'

clipOutput = r'\\Data\MTAP_Data.gdb'
# arcpy.Clip_analysis(OLD,clipInput,clipOutput)
arcpy.Clip_analysis(OLD, clipInput, clipOutput, '#')
arcpy.AddMessage("! Script Completed !")
print( "! Script completed !")

0 Kudos
JoeBorgione
MVP Emeritus

Check the syntax of Clip:  it appears that your clipOutput variable is set to a geodatabase and not a feature class.

That should just about do it....
AndrewSmith
New Contributor III

Thank you for pointing me that way. 

Here is what I did to fix this issue

clipOutput = tempdata +'\Ogma_Clip'

arcpy.Clip_analysis(OGMA, clipInput, clipOutput, '#')

this then created the resultant into my geodatabase. 

0 Kudos
JoeBorgione
MVP Emeritus

Glad it worked.  As a matter of scripting style, I avoid using a + (plus) sign to concatenate variables.  For me the + sign is what you use when you are adding two or more numeric values.

Since you are using 2.x Python, I'd recommend the format() function.  Once you start using 3.x you get the f string method which is even cooler.

clipOutput = tempdata +'\Ogma_Clip'

#becomes

clipOutput = '{}\Ogma_Clip'.format(tempdata)
That should just about do it....
0 Kudos