AnsweredAssumed Answered

How do you loop through a list of files, getting the filenames, and then adding the names to a standalone table?

Question asked by RP.CObb on Sep 16, 2019
Latest reply on Sep 18, 2019 by RP.CObb

Hi,

 

i am relatively new to python but I am trying to work on a script that works in conjunction with modelbuilder and so far things were working up to a point. The issue that I am having is I am trying to loop through a list of files, get the filenames, and update a table with the names from the files. Here is the script that I have currently:

 

 

import arcpy
import os

#Set Parameters
inFiles = arcpy.GetParameterAsText(0)
inTable = arcpy.GetParameterAsText(1)
inField = arcpy.GetParameterAsText(2)
outTable = arcpy.SetParameterAsText(3)

#Iterate through files in folder, insert rows based on file names, and make table view layer
with arcpy.da.InsertCursor(inTable, inField) as cursor:
    #List Files
    flist = arcpy.List(inFiles)

    for f in flist:
       
        #Make table view
        arcpy.MakeTableView_management(inTable)
       
        #Get filename(s)
        f = os.path.basename(flist).rstrip(os.path.splitext(flist)[1])
       
        #Insert values and rows in table
        cursor.insertRow([f])

del cursor

#Add Message
arcpy.AddMessage("This row {0} was added.".format(f))

 

Any help on this would be greatly appreciated.

Outcomes