AnsweredAssumed Answered

Python – Use ‘List’ results as input parameter in arcpy process

Question asked by jrflannery on Oct 16, 2017
Latest reply on Oct 17, 2017 by jrflannery

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:

import arcpy, os, sys, time

# Overwrite existing files
arcpy.env.overwriteOutput = True

#  ********  LOCAL VARIABLES  ********

arcpy.env.workspace = r"C:\Temp\pythonAGOL"

## This will find the three (3) monthly AGOL User list CSV files.
listAGOLusers = arcpy.ListFiles("AGOL_export_users*.csv")
AGOLusers = ("[\"" + '", "'.join(listAGOLusers) + "\"]")

# Name of the FGDB for project tables
listFGDB = arcpy.ListWorkspaces("AGOL*", "FileGDB")
FGDB = (''.join(listFGDB))

# Location and name of table into which monthly CSV user tables are merged
MergedMonthsTable = FGDB + "\\MergedMonthsTable"

#  ********  PROCESSING  ********

# Merge_management (inputs, output, {field_mappings})
arcpy.Merge_management(AGOLusers, MergedMonthsTable)

del listAGOLusers, AGOLusers, listFGDB, FGDB, MergedMonthsTable

 

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 = "AGOL_export_users_2017_06_01.csv"
agolTable2 = "AGOL_export_users_2017_07_03.csv"
agolTable3 = "AGOL_export_users_2017_08_01.csv"

arcpy.Merge_management([agolTable1,agolTable2,agolTable3], MergedMonthsTable)

 

Does anyone see what I am doing incorrectly with ListFiles and join in my script?

Your thoughts are much appreciated.

 

Thank you,

Joe

Outcomes