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']
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.