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

10-16-2017 05:23 AM
Occasional Contributor III

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:


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,


0 Kudos
2 Replies
MVP Legendary Contributor

your error shows that the list object is being returned as a string...

a = ", ".join(['A', 'B', 'C'])

b = a.split(",")

'A, B, C'

['A', ' B', ' C']‍‍‍‍‍‍‍‍‍

You can see how to convert from a list to a string... and back again


it also appears that your list, isn't since it seems to be enclosed in quotes... see this. (carefully note the single and double quote placement.. so close, yet so far)

c = "['A', ' B', ' C']"

Out[10]: "['A', ' B', ' C']"

Out[11]: str
0 Kudos
Occasional Contributor III



Thank you for the python scripting guidance.  I figured out what my problem was.


In part of my script, I am concatenating the results of lists with a string. 


As in:


# Name of the FGDB for project tables

listFGDB = arcpy.ListWorkspaces("AGOL*", "FileGDB")


# Location and name of table into which monthly CSV user tables are merged

MergedMonthsTable = listFGDB + "\\MergedMonthsTable"


Here, the “listFGDB” variable is a list and the list cannot be concatenated with the string item “\\MergedMonthsTable”.  The results equal this error message:

TypeError: can only concatenate list (not "str") to list


So, I had to turn list items into a strings, and that worked.


My mistake was to take the “listAGOLusers” variable and turn it into a string variable and then use that string variable in the arcpy Merge process.  As you pointed out, I used double quotes in the resulting string, which resulted in failure of the script.


In this case, since the a “listAGOLusers” variable is not being concatenated with a string, but instead being used directly in an arcpy process and its structure is already correct, I don’t need to convert the list to a string.


Using the “listAGOLusers” variable in this process works!

arcpy.Merge_management(listAGOLusers, MergedMonthsTable)


Thank you,


0 Kudos