2

I'm using the following tool in ArcGIS 10 - Calculate Distance Band from Neighbor Count (Spatial Statistics). I'm using a python script to execute this tool several times on different shapefiles:

 import arcpy
 arcpy.env.workspace = "c:/data"
 mindist, avgdist, maxdist = arcpy.CalculateDistanceBand_stats("Blocks1", 1, "EUCLIDEAN_DISTANCE")
 mindist, avgdist, maxdist = arcpy.CalculateDistanceBand_stats("Blocks2", 1, "EUCLIDEAN_DISTANCE")
 mindist, avgdist, maxdist = arcpy.CalculateDistanceBand_stats("Blocks3", 1, "EUCLIDEAN_DISTANCE")

At the moment, this outputs the 3 result values to the Results-Messages window in ArcMap 10, overwriting the past results each time. Is there any way I can write these values to a text file after each execution of the tool using python code. If possible I would like to run this on many shapefiles and write the output values to the same text file. If a text file isn't feasible, then anything that can store this information will suffice.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked May 22, 2012 at 12:45

1 Answer 1

3

you can do this with some python bindings as here. You can further enhance the code.

from datetime import datetime
stime = datetime.now()
filename = "path/to/log_%s.txt" % (stime)
if not os.path.exists(filename ):
 os.makedirs(filename) 
f = open(filename, 'a')
f.write(stime + ' : ' + mindist, avgdist, maxdist)
f.close

beside this you can use python logging module too. more information here and here...

import logging
logger = logging.getLogger('myapp')
hdlr = logging.FileHandler('/var/tmp/myapp.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr) 
logger.setLevel(logging.WARNING)
logger.error('We have a problem')
logger.info('While this is just chatty')

Result:

2003年07月08日 16:49:45,896 ERROR We have a problem

i hope it helps you...

answered May 22, 2012 at 13:07
4
  • Thanks for your reply. I'm very new to python. Where in my script (above) would I put this, and do you think it would fit straight in, or would I need to change some things? Commented May 22, 2012 at 13:10
  • you should read A quick tour of ArcPy here. no they are examples code which have to be revised before adding. Commented May 22, 2012 at 13:22
  • Is there not a line of code that just puts 'ResultObject' from the tool into a table or similar? Commented May 22, 2012 at 13:28
  • Is the info you want stored in the results window? help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/… If so do you still want a log file? Commented May 23, 2012 at 19:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.