1

I used a SearchCursor object to create a list containing file paths and saved the list to a variable called FilePathList.

Next, I iterated through elements within the list using a for loop. Within the for loop, I nested an if statement that contains the arcpy.Exists function. I am inserting elements from the FilePathList into the arcpy.Exists function. When list elements are inserted into the function, if the element exists a data object string “1” is created. In addition, if the element does not exist a data object string "0" is created. I read that the arcpy.Exists() function returns a Boolean indicating whether the element exists.

I want to use the arcpy.Exists function to create string objects "1" representing exists and "0" representing does not exist. In the code below, I am able to print the string objects one by one, in the correct order, to the console. However, I would like to append the strings to an empty list in the order that they are printed. The order is important because I will eventually insert the created list into a new field within the dbf file. The field needs to be in the correct order because it corresponds and links to another field.

import arcpy

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

#dbf table used for processing
dbf = "C:\\TestFolder\\ProcessControlTable.dbf"
#Column within dbf table containing file paths 
myColumn = "FI_PATH"
#Temp list created to hold appended items from arcpy.Exists Function
Temp = []

#SearchCursorList created by using SearchCursorObject to create List 
FilePathList =[row[0] for row in arcpy.da.SearchCursor(dbf,myColumn)]
print(FilePathList)
for elements in FilePathList:
    if arcpy.Exists(elements):
        #If element in SearchCursorList Exists;
        #Define a variable called Exists and string object
        Exists = "1"
        Temp.append(Exists[0])
        print(Exists)

    else:
        DoesNotExist = "0"
        Temp.append(Exists[0])
        print(DoesNotExist)

print (Temp)

The output in the Python shell first prints the list containing file paths. Next, strings created from looping with the arcpy.Exists function are printed one by one in the correct order. Lastly, the Temp list is printed however all items in the list are the string object '1'. I want Temp list to contain strings created from the arcpy.Exists function looping in the correct order. The Temp list should print ['1','1','0','1','1','1','1','1','1','0']

Python Shell Output

I'm not sure what code will pull objects out of the loop and put it into a new list. I can't think of the logic with my limited Python and ArcPy knowledge.

1
  • 1
    In your "else", you use a different variable name to store the result than you use to append to the list. This causes the printed values to be different from the list.
    – SMiller
    Commented Jan 15, 2020 at 3:13

2 Answers 2

3

In your "else", you use a different variable name to store the result than you use to append to the list. This causes the printed values to be different from the list.

Some other suggestions:

  1. Naming conventions in Python recommend using lowercase, and in some cases, mixedCase for variables. Some specific recommendations are found in the PEP8 guidelines. https://www.python.org/dev/peps/pep-0008/#prescriptive-naming-conventions
  2. The line Temp.append(Exists[0]) appends the first character of the string contained in the Exists variable. this isn't needed since you want to append a single character string; in other words, you can drop off the [0] in this line.

Incorporating these changes:

import arcpy

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

#dbf table used for processing
dbf = "C:\\TestFolder\\ProcessControlTable.dbf"
#Column within dbf table containing file paths 
myColumn = "FI_PATH"
#Temp list created to hold appended items from arcpy.Exists Function
temp = []

#SearchCursorList created by using SearchCursorObject to create List 
filePathList =[row[0] for row in arcpy.da.SearchCursor(dbf,myColumn)]
print(filePathList)
for elements in filePathList:
    if arcpy.Exists(elements):
        #If element in SearchCursorList Exists;
        #Define a variable called Exists and string object
        exists = "1"    
    else:
        exists = "0"
    # can move these out of the loop since they are performed for both cases
    temp.append(exists) 
    print(exists)

print(temp)
1

Perhaps something like this might serve your eventual purpose:

import arcpy

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

#dbf table used for processing
dbf = "ProcessControlTable.dbf" # you don't need the full path because it's in the current workspace
#Column within dbf table containing file paths 
myColumn = "FI_PATH"

arcpy.AddField_management(dbf,'DoesExist','STRING')
with arcpy.da.UpdateCursor(dbf,[myColumn,'DoesExist']) as uCur:
    for uRow in uCur:
        if arcpy.Exists(uRow[0]):
            uRow[1]='1'
        else:
            uRow[1]='0'
        uCur.updateRow(uRow)

# select the non-existing into a new table
arcpy.TableSelect_analysis(dbf,'DataNotExist.dbf',"DoesExist = '0'")

This adds a field 'DoesExist' to your dbf table then iterates the table recording if the dataset is found in the full path and finally extracts a table for just the files that don't exist. Beware though a 2nd iteration will fail at the add field stage as the field already exists.

Another viable option is using a python dictionary, which stores value pairs, in this case (fullPath,Exists) using the full path for the dataset as the key and exists/not exists as the value:

import arcpy

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

#dbf table used for processing
dbf = "ProcessControlTable.dbf" # you don't need the full path because it's in the current workspace
#Column within dbf table containing file paths 
myColumn   = "FI_PATH"
ExistsDict = {}

with arcpy.da.SearchCursor(dbf,myColumn) as sCur:
    for sRow in sCur:
        if arcpy.Exists(sRow[0]):
            ExistsDict[sRow[0]]='1'
        else:
            ExistsDict[sRow[0]]='0'

for FC in ExistsDict: # loop through each dataset path and print if exists
    print '{} exists value {}'.format(ExistsDict[FC])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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