Using python to execute make x y layer event across all files within a given directory

10-11-2019 04:34 AM
New Contributor

I am trying to create xy event layers that display data from csv tables as points, I would like to loop the normal make x y event layer through the directory that my files are stored in. This data has no georeference and that is accounted for within the code. 

I have created a function that lists out the files that in the directory I need. Then through a series of functions I can create a series of output filenames that correspond with the names of the input files that I have put into the function. 

I keep getting an error message: 

Traceback (most recent call last):
File "C:\Python27\ArcGIS10.6\Lib\site-packages\pythonwin\pywin\framework\", line 326, in RunScript
exec codeObject in __main__.__dict__
File "D:\Documents\", line 47, in <module>
loop_event_layer(csv_list, layer_file)
File "D:\Documents\", line 44, in loop_event_layer
arcpy.MakeXYEventLayer_management(table = i, in_x_field = "X", in_y_field = "Y", out_layer = x, spatial_reference="{B286C06B-0879-11D2-AACA-00C04FA33C20};IsHighPrecision", in_z_field = "PI")
File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\", line 7618, in MakeXYEventLayer
raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: XY Table: Dataset 0 does not exist or is not supported
Failed to execute (MakeXYEventLayer).

The code is below:

import arcpy
import os
#and separates the name at the .csv to give the beginning of the name. The outputs of this new name list have new
#extensions added such as point and raster.
arcpy.env.workspace = "D:/Documents/PhD/GIS/Odhran.gdb/"
path = "C:/Users/MarPAMM/Desktop/Test_with_Ger_for_csv_raster_conversion/"
output = "D:/Documents/PhD/GIS/Odhran.gdb/"
entries = os.listdir(path)
extension = "result.csv"

def list_csv(x):
   csv_list = []
    for i in x:
       if i.endswith(extension):

   return csv_list

csv_list = list_csv(entries)

def split_csv_name(x):
   filename = []
   start = []
      for i in x:
      start, end = i.split(".")
   return filename

 output_file_beginning = split_csv_name(csv_list)
layer_extension = "_layer"
point_extension = "_point"
raster_extension = "_raster"

def join_name(x, extension):
   filename = []
   for i in x:
      filename.append(i + extension)
   return filename
layer_file = join_name(output_file_beginning, layer_extension)

def loop_event_layer(input_file, output_file):
   for i,x in zip(range(len(input_file)),range(len(output_file))):
      arcpy.MakeXYEventLayer_management(table = i, in_x_field = "X", in_y_field = "Y", out_layer = x,       spatial_reference="{B286C06B-0879-11D2-AACA-00C04FA33C20};IsHighPrecision", in_z_field = "PI")
   return x

loop_event_layer(csv_list, layer_file)

0 Kudos
3 Replies
MVP Legendary Contributor


line numbers would help


Throw a print statement in to ensure the path and filenames are correct (ie 'i' or Table)

New Contributor

Thank you for the suggestion.

The print output for list_csv gives the file names for all the files in a numpy array, this section of the code works fine, an example of it can be seen below:

['R59_011_Z41_Copy_result.csv', 'R59_011_Z42_Copy_result.csv', 'R59_011_Z43_Copy_result.csv', 
'R59_011_Z44_Copy_result.csv', 'R59_011_Z46_Copy_result.csv', 'R59_011_Z47_Copy_result.csv', 
'R59_011_Z48_Copy_result.csv', 'R59_011_Z49_Copy_result.csv', 'R59_011_Z50_Copy_result.csv', 
'R59_011_Z52_Copy_result.csv', 'R59_011_Z53_Copy_result.csv', 'R59_011_Z54_Copy_result.csv', 
'R59_011_Z58_Copy_result.csv', 'R59_011_Z59_Copy_result.csv', 'R59_011_Z60_Copy_result.csv', 
'R59_011_Z61_Copy_result.csv', 'R59_011_Z62_Copy_result.csv', 'R59_011_Z63_Copy_result.csv', 
'R59_011_Z64_Copy_result.csv', 'R59_011_Z65_Copy_result.csv', 'R59_011_Z66_Copy_result.csv', 
'R59_011_Z67_Copy_result.csv', 'R59_011_Z68_Copy_result.csv', 'R59_011_Z69_Copy_result.csv', 
'R59_011_Z70_Copy_result.csv', 'R59_011_Z71_Copy_result.csv', 'R59_011_Z72_Copy_result.csv',
'R59_011_Z73_Copy_result.csv', 'R59_011_Z74_Copy_result.csv', 'R59_011_Z75_Copy_result.csv', 
'R59_011_Z76_Copy_result.csv', 'R59_011_Z77_Copy_result.csv', 'R59_011_Z78_Copy_result.csv', 
'R59_011_Z79_Copy_result.csv', 'R59_011_Z80_Copy_result.csv', 'R59_011_Z81_Copy_result.csv', 
'R59_011_Z82_Copy_result.csv', 'R59_011_Z83_Copy_result.csv', 'R59_011_Z84_Copy_result.csv', 

My main issue is using these as a looping input for the arcpy.MakeXYEventLayer_management() function. Looking at the input it might be an idea to just use the full directory rather than the filename and see if it works.

0 Kudos
MVP Legendary Contributor

You will need to set the full path if you haven't set your workspace in the environments of your script

Using environment settings in Python—Geoprocessing and Python | ArcGIS Desktop