I used a SearchCursor Objectobject to create a List ,list
containing file paths, and saved the list to a variable called FilePathListFilePathList
.
Next, I iterateiterated through elements within the list using a forfor
loop. Within the forfor
loop, I nested an ifif
statement that contains the arcpy.Existarcpy.Exists
function. I am inserting elements from the FilePathListFilePathList
into the arcpy.existarcpy.Exists
function. When list elements are inserted into the function;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()arcpy.Exists()
function returns a Boolean indicating whether the element exists.
I want to use the arcpy.Existsarcpy.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 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 pythonPython shell first prints the list containing file paths.
Next, strings created from looping with the arcpy.Existsarcpy.Exists
function are printed one by one in the correct order. Lastly, the Temp ListTemp
list is printed however all items in the list are the string object '1'. I want Temp ListTemp
list to contain strings created from the arcpy.Existarcpy.Exists
function looping in the correct order. The tempTemp
list should print ['1','1','0','1','1','1','1','1','1','0']
ImI'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 pythonPython and arcpyArcPy knowledge.