English

How To: Write the results of geoprocessing tools to a text file using Python

Summary

Instructions provided describe how to write the results of geoprocessing tools to a text file.

When a geoprocessing tool is run, the results are written to the Geoprocessing Results Window in ArcMap or ArcCatalog; they are also written to a history log file. There are times when it is helpful to have the geoprocessing results written to a text file.

Procedure

The following Python script sample runs the Buffer Analysis Tool and then writes the geoprocessing results to a text file.

Code Sample for ArcGIS 10.0:

import arcpy

# Input feature classes to buffer
inFC = "C:\\Temp\\Georgia.shp"

# Output Feature Class
outFC = "C:\\Temp\\Georgia_Buffer.shp"

# Buffer distance
dist = "50 miles"

# Run buffer tool
arcpy.Buffer_analysis(inFC, outFC, dist)

# Write messages to a Text File
txtFile = open("c:\\temp\\GPMessages.txt","w")
txtFile.write (arcpy.GetMessages())
txtFile.close()

Code Sample for ArcGIS 9.2/9.3.x:
import arcgisscripting
gp = arcgisscripting.create()
gp.Toolbox = "Analysis"

# Input feature classes to buffer
inFC = "C:\\Temp\\Georgia.shp"

# Output Feature Class
outFC = "C:\\Temp\\Georgia_Buffer.shp"

# Buffer distance
dist = "50 miles"

# Run buffer tool
gp.buffer(inFC, outFC, dist)

# Write messages to a Text File
txtFile = open("c:\\temp\\GPMessages.txt","w")

txtFile.write (gp.GetMessages())

txtFile.close()

Related Information