Hi there,
I have written some code that reads each line of a text file and adds the lines to a list. It then loops through each item in this list and applies the item to a definition query to select By attribute features in a feature layer. Once its finished looping through the list it copies all selected features to a shapefile.
My concern is that it is extremely slow to process and I am wondering if there is anyway I can do to speed the process up or perhaps a different way I could approach this to speed the script up?
Any ideas on how I could speed up my script would be greatly appreciated...
Here's my code:
# Text files
output = "D:\\Subblocks\\SubblocksOutput.txt"
# Set overwrite property
gp.OverWriteOutput = 1
# Make feature layer
gp.MakeFeatureLayer("D:\\SUBBLOCKS.shp", "feature_lyr")
# Open output file in read only mode
text_file = open(output, "r")
# Loop through text file and add to list
lines = text_file.readlines()
print len(lines)
for line in lines:
print line
query = "\"KEY\"" + " = " + "\'" + line.rstrip("\n") + "\'"
gp.SelectLayerByAttribute_management("feature_lyr", "ADD_TO_SELECTION", query)
# Copy selected features to shapefile
gp.CopyFeatures_management("feature_lyr", "D:\\Subblocks\\SELECTED_SUBBLOCKS.shp")
# Close text file
text_file.close()
# Free memory
del gp