4

Based on my understanding of the ArcGIS path structure, a feature class inside a feature dataset inside a geodatabase can be accessed by the following:

C:\Menu\Lunch.gdb\Sandwich\Cheese

Where Sandwich is the feature dataset and Cheese is the feature class.

In a python script, I want to construct this full path, so that I can check it using arcpy.Exists(x). I have the following variables (pseudocode):

WSpace = C:\Menu\Lunch.gdb\
FDSName = Sandwich
FCName = Cheese

Concatenating WSpace and FDSName gives the correct path to the feature dataset. What I need help with is tacking FCName onto the end. I would think the following code would work, but it renders a space instead of the backslash character:

fullPath = WSpace + FDSName + r"\" + FCName

Instead of r"\", I have also tried "\\", and "/", but they all render a space. This seems like a simple syntax problem.

  • 2
    I would strongly recommend reading the "String literals" section of the Python language reference topic on lexical analysis (how the Python interpreter parses Python source code). – blah238 Feb 7 '13 at 3:34
13

Strings, even when marked as r"Raw strings", cannot end with a slash, it needs to be escaped. Should be:

fullPath = WSpace + FDSName + "\\" + FCName

Or better yet, use os.path.join:

fullPath = os.path.join(WSpace, FDSName, FCName)
  • I tried both "\\" and "//": neither worked for me. But I like os.path.join(), that looks promising. – L_Holcombe Feb 7 '13 at 3:36
  • os.path.join worked perfectly. Thank you for adding that tool to my toolbox. – L_Holcombe Feb 8 '13 at 0:47
1

I don't know why, but r"\" and r'\' both gave me error. Following one works for me:

workspacePath = r'C:\a.gdb'
datasetName = r'dataset'
featureClassName = r'ofc'
featureClassPath = workspacePath+"\\"+datasetName+"\\"+featureClassName
print featureClassPath

output: C:\a.gdb\dataset\ofc

  • 3
    As Jason mentioned, the reason neither r"\" or r'\' works is the single \ escapes the trailing quote, resulting in a syntax error: docs.python.org/2/reference/… – blah238 Feb 7 '13 at 6:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.