[GIS] How to stop ArcGIS Python script printing warnings and other outputs to the console

arcpy

I've written a Python script using arcgisscripting to do various processing steps on some data. I'm planning to run it from a stand-alone command-line window (ie. not within ArcGIS at all) and, as such, have a number of print commands in my code to show its status.

However, when I run it from the console I also get a number of other things printed to the console including warnings (such as a warning that my data has no projection) and the full output of the Nearest Neighbour calculation command. These aren't needed, and will clog up the output (I'll be running this as a batch job with the output piped to a file).

Is there any way to get rid of these? I was hoping for a gp.quiet = true command or a gp.silent = true, but can't seem to find any of those. Any ideas?

Best Answer

I don't think the geoprocessor object provides any clean way of separating out the response type, as most applications would with a log-level: if it does, there isn't anything apparent from the documentation.

I'd use the python logging module (documentation) to log the output you do care about, instead of using print statements:

import logging
LOG_FILENAME = 'example.log'
logging.basicConfig(filename=LOG_FILENAME, level=logging.INFO)
...
logging.info('stuff just happened, better write that down.')

Then use tail -f example.log or equivalent to watch the log.

Related Question