I am solving a location-allocation type of problem where I use a loop to go through several specifications of the problem. The model runs fine, and produces the vizualization outputs just as expected.
At the end of each run I try to append the values from one of the resulting tables to a .txt file using the following script in "Export". The script writes to the file but each time the For loop runs, it overwrites the file instead of appending despite the specification of the code. If I open half-way through the processing I can see the results getting deleted and updated straight after. Can you help me figuring out what is wrong?
import arcpy, csv table = arcpy.GetParameterAsText(0) outfile = arcpy.GetParameterAsText(1) #List of all of the fields in the table fields = arcpy.ListFields(table) field_names = [field.name for field in fields] with open(outfile,'ab') as f: w = csv.writer(f) #Search cursor that will iterate through the rows of the table for row in arcpy.SearchCursor(table): field_vals = [row.getValue(field.name) for field in fields] w.writerow(field_vals)
FINAL EDIT: I have followed the suggestions and changed my model and my script code as below. Now everything works as expected.
import arcpy outfile = arcpy.GetParameterAsText(0) bays = arcpy.GetParameterAsText(1) service = arcpy.GetParameterAsText(2) with open(outfile,'a') as f: f.write(bays + "," + service + "\n")