I am using arcpy.ListFiles(filename*.csv) to automatically find CSV tables in my workspace folder. I am then using ''.join() to turn the list result items into a string to be used as a parameter in an arcpy process.
I have three CSV files in my workspace that are all named like this:
“AGOL_export_users_<date>.csv”
I am trying to use the string result in an arcpy.Merge_management(inputs, output, {field_mappings}) process. The ArcGIS online help for Merge shows this inputs example: ["majorrds.shp", "Habitat_Analysis.gdb/futrds"], where square brackets, double quotes and commas are used to list the items.
My “ListFiles” result when printed looks like this:
['AGOL_export_users_2017_06_01.csv', 'AGOL_export_users_2017_07_03.csv', 'AGOL_export_users_2017_08_01.csv']
My “join” result when printed looks like this:
["AGOL_export_users_2017_06_01.csv", "AGOL_export_users_2017_07_03.csv", "AGOL_export_users_2017_08_01.csv"]
This is my script:
<SPAN class="keyword token">import</SPAN> arcpy<SPAN class="punctuation token">,</SPAN> os<SPAN class="punctuation token">,</SPAN> sys<SPAN class="punctuation token">,</SPAN> time
<SPAN class="comment token"># Overwrite existing files</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
<SPAN class="comment token"># ******** LOCAL VARIABLES ********</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"C:\Temp\pythonAGOL"</SPAN>
<SPAN class="comment token">## This will find the three (3) monthly AGOL User list CSV files.</SPAN>
listAGOLusers <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFiles<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"AGOL_export_users*.csv"</SPAN><SPAN class="punctuation token">)</SPAN>
AGOLusers <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"[\""</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">'", "'</SPAN><SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>listAGOLusers<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\"]"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Name of the FGDB for project tables</SPAN>
listFGDB <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListWorkspaces<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"AGOL*"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"FileGDB"</SPAN><SPAN class="punctuation token">)</SPAN>
FGDB <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">''</SPAN><SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>listFGDB<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Location and name of table into which monthly CSV user tables are merged</SPAN>
MergedMonthsTable <SPAN class="operator token">=</SPAN> FGDB <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\\MergedMonthsTable"</SPAN>
<SPAN class="comment token"># ******** PROCESSING ********</SPAN>
<SPAN class="comment token"># Merge_management (inputs, output, {field_mappings})</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>Merge_management<SPAN class="punctuation token">(</SPAN>AGOLusers<SPAN class="punctuation token">,</SPAN> MergedMonthsTable<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">del</SPAN> listAGOLusers<SPAN class="punctuation token">,</SPAN> AGOLusers<SPAN class="punctuation token">,</SPAN> listFGDB<SPAN class="punctuation token">,</SPAN> FGDB<SPAN class="punctuation token">,</SPAN> MergedMonthsTable<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
When I run the script, I get this error message:
arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: Input Datasets: Dataset '["AGOL_export_users_2017_06_01.csv", "AGOL_export_users_2017_07_03.csv", "AGOL_export_users_2017_08_01.csv"]' does not exist or is not supported
When I list each CSV file as a local variable, the Merge script, shown below, works fine.
agolTable1 <SPAN class="operator token">=</SPAN> <SPAN class="string token">"AGOL_export_users_2017_06_01.csv"</SPAN>
agolTable2 <SPAN class="operator token">=</SPAN> <SPAN class="string token">"AGOL_export_users_2017_07_03.csv"</SPAN>
agolTable3 <SPAN class="operator token">=</SPAN> <SPAN class="string token">"AGOL_export_users_2017_08_01.csv"</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>Merge_management<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN>agolTable1<SPAN class="punctuation token">,</SPAN>agolTable2<SPAN class="punctuation token">,</SPAN>agolTable3<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> MergedMonthsTable<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Does anyone see what I am doing incorrectly with ListFiles and join in my script?
Your thoughts are much appreciated.
Thank you,
Joe