0

I am trying to write file names and paths to two separate fields in a feature class, and the value for each record is dependent on finding a match in a list. When I run my code it writes the same values for each record, so something is going wrong. I think its finidng the first match and writing that value over and over, but I want it to look at the value in the field my search cursor is set to, and then find the match in the list and write that out to the two fields. What am I missing?

import arcpy, os
photos_path = r'P:\Records\GIS\Projects\D04_OHS\OverheadSignStructurePics'
OHS_FC = r'D:\Records\GIS\Projects\D04_OHS\D04_OHS.gdb\D04_OHS'
ID_fld = 'OSMI_ID'
file_nm_fld = 'LINK_FILENAME'
path_fld ='LINK_PATH'
photos_list = []

for dirpath, dirnames, filenames in os.walk(photos_path):
    for filename in filenames:
        fullPath = dirpath+'\\'+filename
        if fullPath not in photos_list:
            photos_list.append(fullPath)

arcpy.AddField_management(OHS_FC,file_nm_fld,'TEXT',100)
arcpy.AddField_management(OHS_FC,path_fld,'TEXT',300)
srch_cursor = arcpy.da.SearchCursor(OHS_FC,ID_fld)
updt_cursor_fn = arcpy.da.UpdateCursor(OHS_FC,file_nm_fld)
updt_cursor_fp = arcpy.da.UpdateCursor(OHS_FC,path_fld)
for row in srch_cursor:
    val = str(row[0])
    for p in photos_list:
        f_name, f_path = os.path.split(p)
        if val in p:
            #print val
             for u_row1 in updt_cursor_fn:
                u_row1[0] = f_name
                updt_cursor_fn.updateRow(u_row1)
             for u_row2 in updt_cursor_fp:
                u_row2[0] = f_path
                updt_cursor_fp.updateRow(u_row2)
1
  • val in p sounds strange to me. imagine val == 1, then it would be in path1, but also in path21 and path1002 etc. Also you should write with a single cursor.
    – radouxju
    Jun 16, 2014 at 18:28

1 Answer 1

3

I don't think your looping is behaving itself. Try using a single update cursor and update 2 fields on it. So something like

fields = (file_nm_fld,path_fld, ID_fld)
 updt_cursor = arcpy.da.updatecursor(OHS_FC,fields)
 for u_row in updt_cursor:
     for p in photos_list:
         if str(row[2]) in p:
             u_row[0] = f_name
             u_row[1] = f_path
             updt_cursor.updaterow(u_row)
0

Your Answer

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

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