Path Names

695
5
Jump to solution
06-17-2014 10:25 AM
BrettAuger1
New Contributor
Hi folks,

I need to get a list of path names for feature classes in a gdb. The gdb is configured something like this:

Myfeatures.gdb
Feature 1 Dataset
[INDENT]fc1, fc2,fc3 [/INDENT]
Feature 2 Dataset
[INDENT]fc1, fc2,fc3 [/INDENT]
Feature 3 Dataset
[INDENT]fc1, fc2,fc3 [/INDENT]

etc.

I am having trouble getting right down to the FC for a path. I tried using the os.path.join function but it doesn't seem to like lists and an input.

This is what I have so far for code.

import arcpy import os # Set the current workspace   gdbInput = "Q:\PIR\GIS\Brett\GDB Import Tool\Test Data\PIR_MR98copy.gdb" gdbOutput = "Q:\PIR\GIS\Brett\GDB Import Tool\Test Data\EmptyGisDeliverables_v14.gdb"  arcpy.env.workspace = gdbInput  # Create list of datasets in input gdb dsInput = arcpy.ListDatasets() #print dsInput  #Create list of fc in ds for ds in dsInput:     fcInput = arcpy.ListFeatureClasses(feature_dataset=ds)     print fcInput


This is what I get for results from previous code:
[u'AppraisalPoint'] [u'BlockHubs', u'BlockPolygon'] [u'RoadLine', u'RoadCrossing', u'RoadNotes', u'RoadHubs'] [u'RiparianLine', u'Riparian_SMZ_Buffer', u'LakeWetland', u'LakeWetlandLine']


Thanks.

Brett
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
IanMurray
Frequent Contributor
Easiest thing to do would to create an empty list outside of your loop, then have each file path append itself to the list.

 import arcpy import os # Set the current workspace   gdbInput = "Q:\PIR\GIS\Brett\GDB Import Tool\Test Data\PIR_MR98copy.gdb" gdbOutput = "Q:\PIR\GIS\Brett\GDB Import Tool\Test Data\EmptyGisDeliverables_v14.gdb"  arcpy.env.workspace = gdbInput  # Create list of datasets in input gdb dsInput = arcpy.ListDatasets() #print dsInput  #make empty list to append file paths to. fclist = []  #Create list of fc in ds for ds in dsInput:     fcInput = arcpy.ListFeatureClasses(feature_dataset=ds)     for fc in fcInput:         filepath = [gdbOutput+ "/" + ds + "/" + fc]         fclist.append(filepath)  arcpy.env.workspace = gdbOutput for path in fclist:     if not arcpy.Exists(path):         print "no {}".format(path)     else:         print "yes {}".format(path) 

View solution in original post

0 Kudos
5 Replies
IanMurray
Frequent Contributor
Really, you just need to iterate through each feature class, and concatenate the workspace, the feature dataset and feature class name together.


import arcpy
import os
# Set the current workspace


gdbInput = "C:\Users\iamurray\Desktop\Test.gdb"
gdbOutput = "Q:\PIR\GIS\Brett\GDB Import Tool\Test Data\EmptyGisDeliverables_v14.gdb"

arcpy.env.workspace = gdbInput

# Create list of datasets in input gdb
dsInput = arcpy.ListDatasets()
#print dsInput

#Create list of fc in ds
for ds in dsInput:
    fcInput = arcpy.ListFeatureClasses(feature_dataset=ds)
    for fc in fcInput:
    
        print gdbInput + "/" + ds + "/" + fc



This gives me

C:\Users\iamurray\Desktop\Test.gdb/Test1/TestFC2
C:\Users\iamurray\Desktop\Test.gdb/Test/TestFC
0 Kudos
BrettAuger1
New Contributor
Really, you just need to iterate through each feature class, and concatenate the workspace, the feature dataset and feature class name together.


import arcpy
import os
# Set the current workspace


gdbInput = "C:\Users\iamurray\Desktop\Test.gdb"
gdbOutput = "Q:\PIR\GIS\Brett\GDB Import Tool\Test Data\EmptyGisDeliverables_v14.gdb"

arcpy.env.workspace = gdbInput

# Create list of datasets in input gdb
dsInput = arcpy.ListDatasets()
#print dsInput

#Create list of fc in ds
for ds in dsInput:
    fcInput = arcpy.ListFeatureClasses(feature_dataset=ds)
    for fc in fcInput:
    
        print gdbInput + "/" + ds + "/" + fc



This gives me

C:\Users\iamurray\Desktop\Test.gdb/Test1/TestFC2
C:\Users\iamurray\Desktop\Test.gdb/Test/TestFC


This seems to work for me too. However when I use a variable to collect the path names and use that variable as an input it only uses the last value. Is there a step that I need to do so the path names are returned in a list that can be used as an input for the next step. This is what I have so far again:

import arcpy
import os
# Set the current workspace


gdbInput = "Q:\PIR\GIS\Brett\GDB Import Tool\Test Data\PIR_MR98copy.gdb"
gdbOutput = "Q:\PIR\GIS\Brett\GDB Import Tool\Test Data\EmptyGisDeliverables_v14.gdb"

arcpy.env.workspace = gdbInput

# Create list of datasets in input gdb
dsInput = arcpy.ListDatasets()
#print dsInput

#Create list of fc in ds
for ds in dsInput:
    fcInput = arcpy.ListFeatureClasses(feature_dataset=ds)
    for fc in fcInput:
        list = [gdbOutput+ "/" + ds + "/" + fc]
        print list

arcpy.env.workspace = gdbOutput
for path in list:
    if not arcpy.Exists(path):
        print "no {}".format(path)
    else:
        print "yes {}".format(path)


Thank you for your help.

Brett
0 Kudos
IanMurray
Frequent Contributor
Easiest thing to do would to create an empty list outside of your loop, then have each file path append itself to the list.

 import arcpy import os # Set the current workspace   gdbInput = "Q:\PIR\GIS\Brett\GDB Import Tool\Test Data\PIR_MR98copy.gdb" gdbOutput = "Q:\PIR\GIS\Brett\GDB Import Tool\Test Data\EmptyGisDeliverables_v14.gdb"  arcpy.env.workspace = gdbInput  # Create list of datasets in input gdb dsInput = arcpy.ListDatasets() #print dsInput  #make empty list to append file paths to. fclist = []  #Create list of fc in ds for ds in dsInput:     fcInput = arcpy.ListFeatureClasses(feature_dataset=ds)     for fc in fcInput:         filepath = [gdbOutput+ "/" + ds + "/" + fc]         fclist.append(filepath)  arcpy.env.workspace = gdbOutput for path in fclist:     if not arcpy.Exists(path):         print "no {}".format(path)     else:         print "yes {}".format(path) 
0 Kudos
BrettAuger1
New Contributor
This works smashingly. Thank you for your help. It is much appreciated.

Easiest thing to do would to create an empty list outside of your loop, then have each file path append itself to the list.


import arcpy
import os
# Set the current workspace


gdbInput = "Q:\PIR\GIS\Brett\GDB Import Tool\Test Data\PIR_MR98copy.gdb"
gdbOutput = "Q:\PIR\GIS\Brett\GDB Import Tool\Test Data\EmptyGisDeliverables_v14.gdb"

arcpy.env.workspace = gdbInput

# Create list of datasets in input gdb
dsInput = arcpy.ListDatasets()
#print dsInput

#make empty list to append file paths to.
fclist = []

#Create list of fc in ds
for ds in dsInput:
    fcInput = arcpy.ListFeatureClasses(feature_dataset=ds)
    for fc in fcInput:
        filepath = [gdbOutput+ "/" + ds + "/" + fc]
        fclist.append(filepath)

arcpy.env.workspace = gdbOutput
for path in fclist:
    if not arcpy.Exists(path):
        print "no {}".format(path)
    else:
        print "yes {}".format(path)

0 Kudos
IanMurray
Frequent Contributor
Glad it worked for you!
0 Kudos