I am wondering why I need to include a '/' at the end of my path name when changing my workspace.

I am writing a script where I'm trying to create a geodatabase and then reassign my workspace to it. I then want to save a subset of a number of feature classes in this geodatabase. Including a '\' at the end of the path works fine and I get four feature class outputs in my newly created geodatabase:

import arcpy
from arcpy.sa import *

workspace = arcpy.env.workspace = 'H:/2018_Conservation/Data/'

arcpy.env.overwriteOutput = True

# 1. Create geodatabase

outFolderPath = workspace
outputGeodatabase = 'Fallow_Verification_FY2018.gdb' 

arcpy.CreateFileGDB_management(outFolderPath, outputGeodatabase)

workspace = workspace + outputGeodatabase + '/' 

# Dictionary to loop through in order to have consistent output filenames
groundTruthFeatureClassDictionary = {'T4_2017_GroundTruth' : 'H:/2017.gdb/RiverT4_2017', 'T1_2018_GroundTruth' : 'H:/T1_2018_fnl', 'T2_2018_GroundTruth' : 'H:/2018.gdb/T2_2018_fnl', 'T3_2018_GroundTruth' : 'H:/2018.gdb/T3_2018_post'

for key, value in groundTruthFeatureClassDictionary.items():
    inFeatures = value
    outFeatureClass = workspace + 'C_' + key
    print(key, value)
    whereClause = "ONAME = 'River, AZ' OR ONAME = 'River, CA'"
    arcpy.Select_analysis(inFeatures, outFeatureClass, whereClause)

However, without the +'/', it still loops through the dictionary, but I merely get one shapefile, C_Fallow_Verification_FY2018.shp' saved in the folder above my geodatabase (/data). My guess is that it's saving over itself (the last iteration of the working version, C_T3_2018_GroundTruth, matches this shapefile), but I don't understand why the key would stop working in distinguishing the names. If there is another more elegant way to reassign the workspace to a newly created geodatabase, I'd be interested to know, but I am also curious as to why the key stops working.

I'm using python 3.4.3.

  • 3
    You don't need to have a slash at the end of your path. You do need to have a slash between path components when you join them, otherwise you get an invalid path. The simplest way is just using os.path.join(path, filename) or os.path.join(path, subfolder_or_gdb, filename) etc... – user2856 Sep 27 '18 at 0:37

Your Answer

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

Browse other questions tagged or ask your own question.