Select to view content in your preferred language

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

5983
23
09-16-2019 05:47 AM
RPGIS
by
Frequent Contributor

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.

0 Kudos
23 Replies
HenryLindemann
Esri Contributor

Hi So in pure python you just want this 

import arcpy

cursor = arcpy.da.InsertCursor('E:/temp/test.gdb/test', ['inField'])

list_of_files = ['file', 'file1', 'file2']

for file in list_of_files:
    cursor.insertRow(['file Path'])

del cursor

Hope it helps

0 Kudos
HenryLindemann
Esri Contributor
import arcpy
import os

# Set Parameters
inFiles = arcpy.GetParameterAsText(0)
inTable = arcpy.GetParameterAsText(1)
inField = arcpy.GetParameterAsText(2) # inField must be in list format ['myField'] 
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:
    # inField must be a list of fields
    
    # List Files
    # a list can be represented as []
    flist = [inFiles]

    for f in flist:
        
        # Get filename(s)
        # if a file name  use split then split on the spaces
        file_name = f.split('/')[-1] # get the last name in the file path 
        arcpy.AddMessage("file to add {0}".format(file_name))

        # Insert values and rows in table
        cursor.insertRow([file_name])

del cursor

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

Thanks Henry,

I was actually able to get the result that I was looking for through my coworker. He went in a different direction but the end result turned out to be what I was looking for(with some minor changes). I didn't consider using the f.split as an option and I think that might have made things a bit simpler for me. I am learning that with python that there simply isn't just one way to go about things, depending on direction and desired outcome. Thank you very much for that example. I will keep practicing using the examples provided as well as testing other options just to see what directions are possible.

0 Kudos
HenryLindemann
Esri Contributor
0 Kudos