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.
Could you use code formatting?
I never used the code formatting before so it's new to me.
Does arcpy.List () exists? I would start with this:
Erstellen von Datenlisten—ArcPy Get Started | ArcGIS Desktop
Thanks Johannes. I did manage to get the script working, however, I get an empty output when I run the tool and so I am not sure what I am doing wrong exactly.
What if you put in a
arcpy.AddMessage
(f) after line 20, does it throws something out? I think your code needs to be "rebuilt", it's easier to do this with hard coded "parameters" rather thanarcpy.GetParameterAsText(0)
A few questions:
Looks like you just need to update the list of files to remove the path, leaving a file name then just append to a new list to use in the InsertCursor. Also, MakeTableView_management is expecting another parameter (you only provided a single "inTable" and it needs the output name of the table view you want to create.
inFiles = ['H:\\file1.txt','H:\\file2.txt','H:\\file3.txt']
inTable = r'\\computer1\home$\user\Docs\ArcGIS\Default.gdb\tabSourceIn'
inField = ['filenames']
rowvalues = []
for file in inFiles:
rowvalues.append(os.path.splitext(os.path.basename(file))[0])
cursor = arcpy.da.InsertCursor(inTable, ['filenames'])
for row in rowvalues:
cursor.insertRow([row])
del cursor
arcpy.MakeTableView_management(inTable, 'outputTableView')
Thanks James,
I was trying to make sense of the parse path script and loop. I am vaguely familiar with loops but the parse path script form the esri documentation wasn't making much sense to me. The reason I was trying to utilize the Make Table View script is because when I ran the script in process within a model, I would receive an error stating the item cannot be opened or it does not exist. I think this link that I was utilizing might have been adding my confusion but I could be wrong.
Parse Path—Help | ArcGIS for Desktop
I am trying to get a better understanding on how python works in terms of context and flow. I have several scripts that I have created that work the way I intended, but when I start to get into developing more complicated script, I start to get a bit confused.
Huge thanks for your help James
-Robert.
Hey James,
So I tested your script and it works but it populates the table with 20+ records, and so I am wondering if there is a way for me to have the table records update based on the number of files added. So if I add 3 files, then the tool adds 3 records to the table with the file names. I am really close to getting where I want to be and so I am working on this little tiny detail.
Thanks,
Robert