Would anyone be able to tell me why the script won't iterate through each text file? The script runs successfully (Python 2.7), but does not populate GasLines_Final polyline feature class with features from each txt file (each txt file resides in PipelinesTxtFiles folder)
import arcpy
# access module to work with ARCGIS
import os
# access OS to access txt files
from arcpy import env
# access environment in ARCGIS
env.workspace = (
"C:/School/SHSU Fall 2022/GEOG5367_GISProgramming/FinalProject/final_project_data/"
)
# set workspace
env.overwriteOutput = True
# turn overwriting on
outpath = "C:/School/SHSU Fall 2022/GEOG5367_GISProgramming/FinalProject/final_project_data/PipelineAnalysis.gdb/"
# set outpath to GDB to store outputs on LOCAL MACHINE
if not arcpy.Exists(outpath):
arcpy.CreateFileGDB_management(
"C:/School/SHSU Fall 2022/GEOG5367_GISProgramming/FinalProject/final_project_data/",
"PipelineAnalysis.gdb",
)
# Create a GDB if one does not exist already
spat_ref = "C:/School/SHSU Fall 2022/GEOG5367_GISProgramming/FinalProject/final_project_data/Gas_Lines.shp"
# use provided Gas_Lines shp spatial reference for this process
arcpy.CreateFeatureclass_management(
outpath,
"GasLines_Final",
"POLYLINE",
"",
"",
"",
"C:/School/SHSU Fall 2022/GEOG5367_GISProgramming/FinalProject/final_project_data/Gas_Lines.shp",
)
# create polyline feature class 'GasLines_Final'
arcpy.AddField_management("PipelineAnalysis.gdb/GasLines_Final", "NAME", "TEXT")
# add a new 'NAME' field to GasLines_Final to store NAME of pipeline built, from txt files
arcpy.AddField_management("PipelineAnalysis.gdb/GasLines_Final", "DATE", "TEXT")
# add a new 'DATE' field to GasLines_Final to store DATE pipeline was created, from txt files
arcpy.AddField_management("PipelineAnalysis.gdb/GasLines_Final", "PSI", "TEXT")
# add a new 'PSI' field to store PSI of pipeline, from txt files; NOTE: PSI was not included in txt files
cursor = arcpy.da.InsertCursor(
"PipelineAnalysis.gdb/GasLines_Final", ["SHAPE@", "NAME", "DATE", "PSI"]
)
# Create an insert cursor for inputting geometry info and field info into GasLines_Final
print("GasLines_Final empty feature class created...")
array = arcpy.Array()
# array for coordinates
point = arcpy.Point()
# create vertex from each coordinate in the line
print("Array defined")
print("Points defined")
name = ""
# emptry string populated from each pipeline NAME from txt file
date = ""
# empty string populated from each new pipeline DATE from txt file
psi = ""
# empty string populated from each new pipeline PSI as it's found; NOTE: PSI not included in txt file
print("Empty lists created")
PipelineFiles = os.listdir(
"C:/School/SHSU Fall 2022/GEOG5367_GISProgramming/FinalProject/final_project_data/PipelineTxtFiles"
) # set a variable to call list of Pipeline txt files
# loop through the text files in PipelineTxtFiles folder in my LOCAL MACHINE
for file in PipelineFiles:
# open PipelineTxtFiles
txtFile = open(
"C:/School/SHSU Fall 2022/GEOG5367_GISProgramming/FinalProject/final_project_data/PipelineTxtFiles/{}".format(
file
),
"r+",
)
for line in txtFile: # loop through each open text file
splitline = line.split(" ") # convert each line into a list of the words
if splitline[0] == "Work":
date = splitline[3] # if the first word = work, change date to new date
elif splitline[0] == "Spatial":
spatialreference = arcpy.Describe(
spat_ref
).spatialReference # define spatial reference of pipeline
elif splitline[0] == "Name:":
name = splitline[
1
] # if the first word = Name:, change name of gasline to new gas line name
elif splitline[0] == "PSI":
psi = splitline[
1
] # if the first word = PSI, change PSI variable to new psi; NOTE: PSI info not included in txt files
elif splitline[0] == "\n":
polyline = arcpy.Polyline(
array
) # if line is blank, create a polyline out of the array
cursor.insertRow(
[polyline, name, date, psi]
) # insert polyline, gasline name, gasline date, and gasline PSI into GasLines_Final
array = (
arcpy.Array()
) # reset the array, so a new array is created when the current file is done being used
else:
ex = splitline[0] # select the first word in x-point
point.X = ex[:-1] # remove the comma from end of x-point
point.Y = splitline[1] # select the second word in y-point
array.add(point) # add the x,y point to the array
txtFile.close() # close the open text file
del cursor
# delete the cursor
fc_projection = spatialreference
print("Spatial reference is {}".format(spatialreference.name))
# print("spatial reference, so user knows what is being used, and if it needs to change")
print(
"GasLines_Final finished creating... Check results in GasLines_Final feature class in PipelineAnalysis.gdb"
)
# print("message letting user know process was complete")
1 Answer 1
Try this:
import arcpy
# access module to work with ARCGIS
import os
import glob
# access OS to access txt files
from arcpy import env
# access environment in ARCGIS
basedir = (
"C:/School/SHSU Fall 2022/GEOG5367_GISProgramming/FinalProject/final_project_data"
)
env.workspace = basedir
# set workspace
env.overwriteOutput = True
# turn overwriting on
outpath = os.path.join(basedir, "PipelineAnalysis.gdb")
# set outpath to GDB to store outputs on LOCAL MACHINE
if not arcpy.Exists(outpath):
arcpy.CreateFileGDB_management(
basedir,
"PipelineAnalysis.gdb",
)
# Create a GDB if one does not exist already
spat_ref = os.path.join(basedir, "Gas_Lines.shp")
# use provided Gas_Lines shp spatial reference for this process
arcpy.CreateFeatureclass_management(
outpath,
"GasLines_Final",
"POLYLINE",
"",
"",
"",
os.path.join(basedir, "Gas_Lines.shp"),
)
# create polyline feature class 'GasLines_Final'
arcpy.AddField_management("PipelineAnalysis.gdb/GasLines_Final", "NAME", "TEXT")
# add a new 'NAME' field to GasLines_Final to store NAME of pipeline built, from txt files
arcpy.AddField_management("PipelineAnalysis.gdb/GasLines_Final", "DATE", "TEXT")
# add a new 'DATE' field to GasLines_Final to store DATE pipeline was created, from txt files
arcpy.AddField_management("PipelineAnalysis.gdb/GasLines_Final", "PSI", "TEXT")
# add a new 'PSI' field to store PSI of pipeline, from txt files; NOTE: PSI was not included in txt files
cursor = arcpy.da.InsertCursor(
"PipelineAnalysis.gdb/GasLines_Final", ["SHAPE@", "NAME", "DATE", "PSI"]
)
# Create an insert cursor for inputting geometry info and field info into GasLines_Final
print("GasLines_Final empty feature class created...")
array = arcpy.Array()
# array for coordinates
point = arcpy.Point()
# create vertex from each coordinate in the line
print("Array defined")
print("Points defined")
name = ""
# emptry string populated from each pipeline NAME from txt file
date = ""
# empty string populated from each new pipeline DATE from txt file
psi = ""
# empty string populated from each new pipeline PSI as it's found; NOTE: PSI not included in txt file
print("Empty lists created")
PipelineFiles = glob.glob(os.path.join(basedir, "PipelineTxtFiles", "*.txt"))
# loop through the text files in PipelineTxtFiles folder in my LOCAL MACHINE
for file in PipelineFiles:
# open PipelineTxtFiles
with open(file, "r+") as txtFile:
for line in txtFile: # loop through each open text file
# convert each line into a list of the words
splitline = line.split(" ")
if splitline[0] == "Work":
# if the first word = work, change date to new date
date = splitline[3]
elif splitline[0] == "Spatial":
# define spatial reference of pipeline
spatialreference = arcpy.Describe(spat_ref).spatialReference
elif splitline[0] == "Name:":
# if the first word = Name:, change name of gasline to new gas line name
name = splitline[1]
elif splitline[0] == "PSI":
# if the first word = PSI, change PSI variable to new psi; NOTE: PSI info not included in txt files
psi = splitline[1]
elif splitline[0] == "\n":
# if line is blank, create a polyline out of the array
polyline = arcpy.Polyline(array)
# insert polyline, gasline name, gasline date, and gasline PSI into GasLines_Final
cursor.insertRow([polyline, name, date, psi])
# reset the array, so a new array is created when the current file is done being used
array = arcpy.Array()
else:
ex = splitline[0] # select the first word in x-point
point.X = ex[:-1] # remove the comma from end of x-point
point.Y = splitline[1] # select the second word in y-point
array.add(point) # add the x,y point to the array
del cursor
# delete the cursor
fc_projection = spatialreference
print("Spatial reference is {}".format(spatialreference.name))
# print("spatial reference, so user knows what is being used, and if it needs to change")
print(
"GasLines_Final finished creating... Check results in GasLines_Final feature class in PipelineAnalysis.gdb"
)
# print("message letting user know process was complete")
os.listdir
is probably not the way to go for listing files as it will also list directories.
You'd better use glob
to search for file using pattern matching.
Also, store your base directory as a variable and use os.path.join
to construct subfolders instead of typing the full path again and again. It's error prone.
In addition, you'd better use a context manager when opening files.
Finally, use brackets with print. Even with Python 2.