AnsweredAssumed Answered

loop through a folder with text files

Question asked by Mark_S_B on Jan 30, 2020
Latest reply on Feb 13, 2020 by bixb0012

Hi all

 

I am stuck with how to loop through a folder containing numerous text files containing lat and long values. The idea is to open a text file, create an XY layer and then append the result into a layer within a geodatabase. I can do the following which takes a single text file, creates the xy layer and then appends this xy layer to the geodatabase layer:

 

# XYTableToPoint.py
# Description: Creates a point feature class from input table

# import system modules
import arcpy

# Set environment settings
arcpy.env.workspace = r"C:\Projects\points\test.gdb"

# Set the local variables
in_table = r"D:\points\split_data\file_1.txt"
out_feature_class = "file1"
x_coords = "field7"
y_coords = "field6"
z_coords = ""

# Make the XY event layer...
arcpy.management.XYTableToPoint(in_table, out_feature_class,x_coords, y_coords, z_coords,
arcpy.SpatialReference(4326))

# Print the total rows
print(arcpy.GetCount_management(out_feature_class))

arcpy.management.Append("file1", r"C:\Projects\points\test.gdb\all_Points", "NO_TEST", 'id "id" true true false 25 Text 0 0,First,#,file1,Field1,0,8000;individualCount "individualCount" true true false 15 Text 0 0,First,#,file1,Field2,0,8000;year "year" true true false 4 Long 0 0,First,#,file1,Field3,-1,-1;month "month" true true false 2 Short 0 0,First,#,file1,Field4,-1,-1;country "country" true true false 255 Text 0 0,First,#,file1,Field5,0,8000;decimalLatitude "decimalLatitude" true true false 8 Double 0 0,First,#,file1,Field6,-1,-1;decimalLongitude "decimalLongitude" true true false 8 Double 0 0,First,#,file1,Field7,-1,-1;Name "Name" true true false 100 Text 0 0,First,#,file1,Field8,0,8000', '', '')

 

This works fine but this would be tedious to do edit and rerun manually for each file (potentially over a 1000 files).

 

Any advice most welcome.

Mark

Outcomes