0

I have a list object that contains file paths as text. I named the list object filepathlist. An example of a file path within the list is C:\TestFolder\Folder1\A\address_pt.shp. The file paths lead to a directory where some folders contain shapefiles.

I want to use the list object,filepathlist, and iterate through each element of the list passing the list into the arcpy.ListFields function to create multiple lists of field names for each shapefile. I'm not sure if it's possible, but maybe even a list containing multiple lists within.

I want to create lists of Field names for each shapefile in my directory because I ultimately plan to use the list of fields generated from the arcpy.ListFields function and compare the lists to another list that contains field names thought to be within the shapefile. I aim to ultimately solve the problem and question:

Does my list of fields found in the shapefile match the list of fields thought to be within the shapefile?

However, my question and first problem is trying to figure out the logic and code of how to iterate through a list of file paths within the arcpy.ListFields function and return lists of Fields within each shapefile.

I'm not sure if i'm taking the right approach in trying to solve the problem. I have included code below to try and explain what i'm trying to do.

import arcpy

arcpy.env.workspace = r"C:\TestFolder"

#dbf File Process control Table 
dbf = "ProcessControlTable.dbf"

#File Path of shapefile
shapefile = "C:\\TestFolder\\Folder1\\F1\\address_pt.shp"

#Define Field within dBASE Table that holds File Paths
myfield = "FI_PATH"

#Print List of Field Names for shapefile defined
print([field.name for field in arcpy.ListFields(shapefile)])


#List of file paths with list comprehension  
filepathlist =[row[0] for row in arcpy.da.SearchCursor(dbf,myfield)]
print(filepathlist)

#Use arcpy.ListFields function to iterate through filepaths list 
lstfields = arcpy.ListFields(filepathlist)
for row in lstFields:
    #Create a list of Field Names for each Shapefile found in filepathlist
    #Save the field names object list for each shapefile so it can be compared to another list
    #Print a list of Field Names in each Shapefile

Basically, I only know how to list the fields from one shapefile. I would like some guidance on where I can take the logic and code to possibly iterate through a list of file paths and list fields for each shapefile at the path location.

4

Your code is quite good up to here:

lstfields = arcpy.ListFields(filepathlist)
for row in lstFields:
    #Create a list of Field Names for each Shapefile found in filepathlist
    #Save the field names object list for each shapefile so it can be compared to another list
    #Print a list of Field Names in each Shapefile

I think a dictionary would help you here:

DS_FieldDict = {} # a new, empty dictionary
# loop through the file list
for thisFile in filepathlist:
    # get all the field names
    ContainedFields = [f.name for f in arcpy.ListFields(thisFile)]
    # pack them into a dictionary
    DS_FieldDict[thisFile] = ContainedFields

# use the dict in a meaningful manner
for ThisFC in DS_FieldDict:
    print '{} has fields {}'.format(ThisFC,','.join(DS_FieldDict[ThisFC]))

This should get you close, you can then read the list of the fields the file is thought to have and verify that the field name exists - but beware python is case sensitive so you might want to use [f.name.upper() for f in arcpy.ListFields(thisFile)] to pack the field names then convert your comparison string to uppercase for checking. You are guaranteed in any dataset that a name can only exist once in upper, lower or any other case of characters.

2

Assuming that you see the expected list of paths printed when you run your presented code, then your next step will be to obtain a list of the shapefiles in each folder path, which you can do using arcpy.ListFeatureClasses().

After:

filepathlist =[row[0] for row in arcpy.da.SearchCursor(dbf,myfield)]
print(filepathlist)

try writing:

for path in filePathList:
    print(path)
    shpFileList = arcpy.ListFeatureClasses()
    print(shpFileList)
    for shpFile in shpFileList:
        print(shpFile)

This will tell you that you have successfully found each shapefile in your list of file paths, unless you have not.


It is telling that you say:

An example of a file path within the list is C:\TestFolder\Folder1\A\address_pt.shp. The file paths lead to a directory where some folders contain shapefiles.

C:\TestFolder\Folder1\A\address_pt.shp is the name of a shapefile. It is not a folder that contains shapefiles.

Below is a picture of a folder that contains two shapefiles named C:\polygeo\Data\ne_10m_admin_0_countries.shp and C:\polygeo\Data\ne_10m_admin_1_states_provinces.shp. The name of the folder that contains them is C:\polygeo\Data.

enter image description here

2
  • I am able to print the list of paths, however I am not able to obtain a list of the shapefiles in each folder path. Empty lists are printed for each folder path. C:\TestFolder\Folder1\A\address_pt.shp [] Possibly, because I have paths in my list that do not exist and no shapefile is found. Jan 18 '20 at 23:34
  • Alright, I am pointing to an individual shapefile with the file path and not to a folder containing features. That is why there are no feature classes in the list. I will use arcpy.ListFields and list the fields of the shapefile. Jan 19 '20 at 0:21

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.