The basic premise is as follows: look in a folder and find all feature classes (within file GDBs), use a for loop to isolate feature classes one at a time and subsequently isolate date attributes, iterate through and update all the date-related attributes to a given value (currently set at 9/9/9999).
The code will run, but nothing happens. The print statements I have embedded won't even return anytihng. I think the error is somewhere up at the beginning.
import arcpy
import os
try:
def fcs_in_workspace(workspace):
arcpy.env.workspace = workspace
for fc in arcpy.ListFeatureClasses():
yield os.path.join(workspace, fc)
for ws in arcpy.ListWorkspaces():
for fc in fcs_in_workspace(os.path.join(workspace, ws)):
yield fc
for fc in fcs_in_workspace("path to folder of GDBs"):
allFields = arcpy.ListFields(fc)
stringFields = []
for field in allFields:
if field.type == "Date" and "flex" not in field.name:
stringFields.append(field.name)
print(str(field.name))
stringCount = len(stringFields)
with arcpy.da.UpdateCursor(fc, stringFields) as curU:
for row in curU:
rowU = row
for fields in range(stringCount):
# change below to None or ''
if rowU[fields] == '':
rowU[fields] = "9/9/9999"
elif rowU[fields] == None:
rowU[fields] = "9/9/9999"
curU.updateRow(rowU)
del curU
print("Updated Dates for " + str(fc))
except Exception as e:
print("Error: " + e.args[0])
1 Answer 1
Here's my solution to the first part of your script. Maybe combining this with your UpdateCursor will help? I think you may be grabbing the fields wrong. Let me know if this works! If not, I can do some more debugging tomorrow. FYI the feature classes I was testing didn't have date fields, so I just called String fields instead, so you'll have to change that part of the script.
import arcpy
import os
try:
workspace = arcpy.env.workspace = r"FOLDER"
gdb_list = arcpy.ListWorkspaces("*", "FileGDB")
fc_list = []
allfields = []
stringFields = []
for dirpath, dirnames, datatypes in arcpy.da.Walk(workspace, datatype = "FeatureClass"):
for i in datatypes:
fc_list.append(os.path.join(dirpath, i))
#print (fc_list)
for feature in fc_list:
field_list = arcpy.ListFields(feature)
for field in field_list:
#print(field.type)
if field.type == "String":
stringFields.append(field.name)
print(stringFields)
except Exception as e:
print(e)
print(arcpy.GetMessages())
for
loop, meaningfcs_in_workspace()
isn't yielding anything. Add some print statements in there, make sure the folder path you're passing in is valid, etc.