In ArcGIS 10.5
I have a python script that works well in the python window, however, I am trying to integrate it in a tool, mainly to make use of the arcpy.GetParametersAsText()
input feature for the input and output file names/paths.
The basic intent of the script is to output a number of attribute fields from a particular feature class into a very specific format for a very specific text file (.PTS survey line file for WinFrog if anyone knows it).
Initially, it was failing due to a Unicode error when trying to .write()
to the file object created with .open()
. As you can see below I have tried numerous things to ensure that the file object 'outfile' is indeed a regular file object, and not Unicode, that is only defined by a path in the from of a string.
Anyway, now the script will run and complete 'successfully', creating the file in the directory indicated in the tool/script input. But now it is not populated with any data! Again the script works every time when running from the python window. Any help would be awesome, even if its just identifying the problem. Fully understand this is user error and that I'm likely overlooking something simple.
Script below, sorry for any display issues, new to Stack Overflow.
import arcp
fcpath = str(arcpy.GetParameterAsText(0))
fc = "r\"" + fcpath + "\""
fields = ["LineName", "Lat_SOL" , "Lon_SOL", "Lat_EOL", "Lon_EOL", "Northing_SOL", "Easting_SOL", "Northing_EOL", "Easting_EOL", "Shape_Length"]
filelocation = str(arcpy.GetParameterAsText(1))
outfile = open("r\"" + filelocation + ".PTS", 'w')
with arcpy.da.SearchCursor(fc, fields) as cursor:
for row in cursor:
r1 = ("0,{0},0,0.000,0.000,1,2,65280,0,0.200,0,0,1.000,1,0\n".format(row[0]))
r2 = ("1,{0},{1},0.0m,0.0m,{2},{3},0.000\n".format(row[1], row[2], row[5], row[6]))
r3 = ("1,{0},{1},0.0m,0.0m,{2},{3},{4}\n".format(row[3], row[4], row[7], row[8], row[9]))
outfile.write(r1 + r2 + r3)
Update per MikeWatt's suggestions, still not working.
import arcpy, os
fcpath = arcpy.GetParameterAsText(0)
fields = ["LineName", "Lat_SOL" , "Lon_SOL", "Lat_EOL", "Lon_EOL", "Northing_SOL", "Easting_SOL", "Northing_EOL", "Easting_EOL", "Shape_Length"]
outfile = open(arcpy.GetParameterAsText(1) + ".PTS", 'w')
with arcpy.da.SearchCursor(fc, fields) as cursor:
for row in cursor:
r1 = ("0,{0},0,0.000,0.000,1,2,65280,0,0.200,0,0,1.000,1,0\n".format(row[0]))
r2 = ("1,{0},{1},0.0m,0.0m,{2},{3},0.000\n".format(row[1], row[2], row[5], row[6]))
r3 = ("1,{0},{1},0.0m,0.0m,{2},{3},{4}\n".format(row[3], row[4], row[7], row[8], row[9]))
outfile.write(r1 + r2 + r3)
outfile.close()
latest attempt to get anything into the output file. Still nothing. Very strange again as this script works no worries when copied straight into the ArcGIS python window.
import arcpy
fcpath = arcpy.GetParameterAsText(0)
fields = ["LineName", "Lat_SOL" , "Lon_SOL", "Lat_EOL", "Lon_EOL", "Northing_SOL", "Easting_SOL", "Northing_EOL", "Easting_EOL", "Shape_Length"]
outfile = open(arcpy.GetParameterAsText(1) + ".PTS", 'w')
outfile.write("TEST\n" + "Test\n" + "TEST\n")
outfile.close()
The attached ScreenGrab is the Parameters window of the 'Add Script' tool. I could be messing this aspect of it up as well.
GetParameterAsText()
. Also it's good practice to explicitly close the file after writing to ensure it gets flushed to disk, either withoutfile.close()
or using awith
block to open it.outfile.write("test\n"+"test2\n"+"test3\n")
instead of the cursor.row