Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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. Any pointers would be appreciated.

share|improve this question
1  
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

2 Answers 2

up vote 7 down vote accepted

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)
share|improve this answer
    
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

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

share|improve this answer
2  
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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