I have files in subfolders on a network drive. I want to create a list of files, and insert that list into an Oracle table.
I can successfully iterate through the files in the subfolders and do the insert:
- Loop through a user-defined list of subfolders called
subfolder_list
(I only want the files in specific subfolders; not all of the subfolders) - Loop through the files in each subfolder
- Using an ArcSDESQLExecute class in the ArcPy site package, insert each file name into the
records
table
import arcpy
import os
db_conn_folder = r'C:\connection\folder\connection.sde'
conn = arcpy.ArcSDESQLExecute(db_conn_folder)
folder = r'\\my\network\folder'
subfolder_list = [r'\DRAWINGS', r'\DOCUMENTS', r'\VIDEOS']
#Delete all records in the table
db_exec = conn.execute('delete from records')
#Loop through the list of subfolders
for i in subfolder_list:
#loop through the files in the subfolder
for filename in os.listdir(folder + i):
#insert the file names into the table
db_exec = conn.execute('insert into records (file_name) values (' + filename + '\')')
#Commit the insert statements
db_exec = conn.execute('commit')
print "Complete."
The SQL is a little hard to read with all the python parsing and formatting. Plain-language SQL would look like this:
delete from records;
insert into records (file_name) values ('plan_profile_1.tif');
commit;
While the code works just fine, I'm wondering, can it be improved?
(I can count the number of Python scripts I've written on one hand.)