<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Make XY Event Layer for all files in directory in Geoprocessing Questions</title>
    <link>https://community.esri.com/t5/geoprocessing-questions/make-xy-event-layer-for-all-files-in-directory/m-p/1032739#M25247</link>
    <description>&lt;P&gt;&lt;A href="https://community.esri.com/t5/python-blog/code-formatting-the-community-version/ba-p/1007633" target="_blank"&gt;Code formatting ... the Community Version - Esri Community&lt;/A&gt;&lt;/P&gt;&lt;P&gt;your try - except block isn't within the loop&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import os

mydir = r"I:\gas\export\gps_test"
# Set environment settings
arcpy.env.workspace = mydir
for filename in os.listdir(mydir):
    if filename.endswith(".csv") or filename.endswith(".xlsx"):
        print(os.path.join(mydir, filename))
    else:
        continue
    # Set the local variables
    in_table = filename
    out_layer = "gps_" + filename
    saved_layer = mydir
    x_coords = "Easting"
    y_coords = "Northing"
    z_coords = "Elevation"
    # Set the spatial reference
    spRef = arcpy.SpatialReference("NZGD 1949 Hawkes Bay Circuit")
    # Make the XY event layer
    arcpy.MakeXYEventLayer_management(in_table, x_coords, y_coords, out_layer, spRef, z_coords)

    # Print the total rows
    print(arcpy.GetCount_management(out_layer))
    # Save to a layer file
    arcpy.SaveToLayerFile_management(out_layer, saved_layer)
    # Copy features to shapefile
    arcpy.CopyFeatures_management(out_layer, out_layer.strip(".shp"))&lt;/LI-CODE&gt;&lt;P&gt;quick format... dumped the try-except block.&amp;nbsp; If it fails, you will know about it and you will get the same error message in any event&lt;/P&gt;</description>
    <pubDate>Thu, 04 Mar 2021 01:58:19 GMT</pubDate>
    <dc:creator>DanPatterson</dc:creator>
    <dc:date>2021-03-04T01:58:19Z</dc:date>
    <item>
      <title>Make XY Event Layer for all files in directory</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/make-xy-event-layer-for-all-files-in-directory/m-p/1032734#M25246</link>
      <description>&lt;P&gt;Hi there,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to create xy event layer that display data from csv tables as points, set new projected system, copy features to shapefiles. I am new to Python, the script below is working BUT only for 1 file, the last one printed, and I don't know how to do that for all files in directory. Thank you.&lt;/P&gt;&lt;P&gt;import arcpy&lt;BR /&gt;import os&lt;/P&gt;&lt;P&gt;mydir = r"I:\gas\export\gps_test"&lt;BR /&gt;for filename in os.listdir(mydir):&lt;BR /&gt;&amp;nbsp; &amp;nbsp; if filename.endswith(".csv") or filename.endswith(".xlsx"):&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; print(os.path.join(mydir, filename))&lt;BR /&gt;else:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; continue&lt;/P&gt;&lt;P&gt;# Set environment settings&lt;BR /&gt;arcpy.env.workspace = mydir&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;try:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; # Set the local variables&lt;BR /&gt;&amp;nbsp; &amp;nbsp; in_table = filename&lt;BR /&gt;&amp;nbsp; &amp;nbsp; out_layer = "gps_" + filename&lt;BR /&gt;&amp;nbsp; &amp;nbsp; saved_layer = mydir&lt;BR /&gt;&amp;nbsp; &amp;nbsp; x_coords = "Easting"&lt;BR /&gt;&amp;nbsp; &amp;nbsp; y_coords = "Northing"&lt;BR /&gt;&amp;nbsp; &amp;nbsp; z_coords = "Elevation"&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; # Set the spatial reference&lt;BR /&gt;&amp;nbsp; &amp;nbsp; spRef = arcpy.SpatialReference("NZGD 1949 Hawkes Bay Circuit")&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; # Make the XY event layer&lt;BR /&gt;&amp;nbsp; &amp;nbsp; arcpy.MakeXYEventLayer_management(in_table, x_coords, y_coords, out_layer, spRef, z_coords)&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; # Print the total rows&lt;BR /&gt;&amp;nbsp; &amp;nbsp; print(arcpy.GetCount_management(out_layer))&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; # Save to a layer file&lt;BR /&gt;&amp;nbsp; &amp;nbsp; arcpy.SaveToLayerFile_management(out_layer, saved_layer)&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; # Copy features to shapefile&lt;BR /&gt;&amp;nbsp; &amp;nbsp; arcpy.CopyFeatures_management(out_layer, out_layer.strip(".shp"))&lt;BR /&gt;&lt;BR /&gt;except Exception as err:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; print(err.args[0])&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Mar 2021 01:31:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/make-xy-event-layer-for-all-files-in-directory/m-p/1032734#M25246</guid>
      <dc:creator>Pokatko</dc:creator>
      <dc:date>2021-03-04T01:31:39Z</dc:date>
    </item>
    <item>
      <title>Re: Make XY Event Layer for all files in directory</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/make-xy-event-layer-for-all-files-in-directory/m-p/1032739#M25247</link>
      <description>&lt;P&gt;&lt;A href="https://community.esri.com/t5/python-blog/code-formatting-the-community-version/ba-p/1007633" target="_blank"&gt;Code formatting ... the Community Version - Esri Community&lt;/A&gt;&lt;/P&gt;&lt;P&gt;your try - except block isn't within the loop&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import os

mydir = r"I:\gas\export\gps_test"
# Set environment settings
arcpy.env.workspace = mydir
for filename in os.listdir(mydir):
    if filename.endswith(".csv") or filename.endswith(".xlsx"):
        print(os.path.join(mydir, filename))
    else:
        continue
    # Set the local variables
    in_table = filename
    out_layer = "gps_" + filename
    saved_layer = mydir
    x_coords = "Easting"
    y_coords = "Northing"
    z_coords = "Elevation"
    # Set the spatial reference
    spRef = arcpy.SpatialReference("NZGD 1949 Hawkes Bay Circuit")
    # Make the XY event layer
    arcpy.MakeXYEventLayer_management(in_table, x_coords, y_coords, out_layer, spRef, z_coords)

    # Print the total rows
    print(arcpy.GetCount_management(out_layer))
    # Save to a layer file
    arcpy.SaveToLayerFile_management(out_layer, saved_layer)
    # Copy features to shapefile
    arcpy.CopyFeatures_management(out_layer, out_layer.strip(".shp"))&lt;/LI-CODE&gt;&lt;P&gt;quick format... dumped the try-except block.&amp;nbsp; If it fails, you will know about it and you will get the same error message in any event&lt;/P&gt;</description>
      <pubDate>Thu, 04 Mar 2021 01:58:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/make-xy-event-layer-for-all-files-in-directory/m-p/1032739#M25247</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-03-04T01:58:19Z</dc:date>
    </item>
    <item>
      <title>Re: Make XY Event Layer for all files in directory</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/make-xy-event-layer-for-all-files-in-directory/m-p/1032740#M25248</link>
      <description>&lt;P&gt;Ooh I see! Thank you Dan.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Mar 2021 02:01:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/make-xy-event-layer-for-all-files-in-directory/m-p/1032740#M25248</guid>
      <dc:creator>Pokatko</dc:creator>
      <dc:date>2021-03-04T02:01:47Z</dc:date>
    </item>
  </channel>
</rss>

