English

Error: Print statements inside an in-process script tool cause error

Error Message

Running in-process python scripts in ArcToolbox produces the following error:

"<type 'exceptions.IOError'>: [Errno 9] bad file descriptor".

Cause

Print statements inside a loop within the script; for example, when using Cursors.

Code:
import arcgisscripting
gp = arcgisscripting.create(9.3)

for x in range(1,2500):
gp.addmessage(str(x)) # this line only necessary to track where it fails
print x

Solution or Workaround

The following are known solutions to this issue:

  • Comment out or remove the print statements.
  • Wrap the print statements in a try/except, such as in the example below:

    Code:
    try:
    print "some statement"
    except IOError:
    pass