0

I want to convert my .txt files to shapefile. I want the system to be able to move to the next .txt file in the folder, other than me inputting one after the other. This is whatI have done but I have to input the files one after the other which takes a lot of time

 import arcpy

 newfcName ="532724.shp"
 outpath = r"C:/FLOOD/IKEJA AND KOSOFE"

# Declaration
arcpy.env.overwriteOutput=True
arcpy.env.workspace= outpath


# Create new Shapefile and add FIELDS
newfc = arcpy.CreateFeatureclass_management(outpath, newfcName, "Point")
arcpy.AddField_management(newfc, "X", "STRING", field_length = 50)
arcpy.AddField_management(newfc, "Y", "STRING", field_length = 50)
arcpy.AddField_management(newfc, "Z", "STRING", field_length = 50)
arcpy.AddField_management(newfc, "I", "FLOAT", field_length = 50)

# Reference Cursors
cursor=arcpy.da.InsertCursor(newfc, [ "SHAPE@XY", "X", "Y", "Z", "I"])

# Read File 
a = open("C:/FLOOD/IKEJA AND KOSOFE/DEM/532724.txt","r")
inputF = a.readlines()

for line in inputF:
xCoordinate, yCoordinate, zValue, iValue = line.split(" ")
xy = (float(xCoordinate), float(yCoordinate))
newRow = (xy, str(xCoordinate), str(yCoordinate), str(zValue), float(iValue))
cursor.insertRow(newRow)

a.close()
infc = r"C:/FLOOD/IKEJA AND KOSOFE/532724.shp"
sr = arcpy.SpatialReference("Minna UTM Zone 31N")
arcpy.DefineProjection_management(infc, sr)
  • Shapefile name should not start with numbers. Please refer to this question regarding naming convention: gis.stackexchange.com/questions/7886/… – ahmadhanb Aug 11 '17 at 14:38
  • indent the contents of the 'for' loop. Most other languages indenting inside a loop is a convention, but with python, it's required. – P.T. Curran Oct 6 '17 at 12:41
1

You just need to iterate over the files in the directory.

See the code here: https://stackoverflow.com/questions/10377998/how-can-i-iterate-over-files-in-a-given-directory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.