"IOError: [Errno 9] Bad file descriptor" using print statement in Arc 10

Discussion created by AndrewE on Aug 1, 2011
Latest reply on Mar 28, 2013 by kass3

I am having some trouble with a simple Python script, and cannot figure out why. Hopefully I can explain this...

# Script arguments
TempMXD = arcpy.GetParameterAsText(0) # MXD Document (used as template)
Layers = arcpy.GetParameterAsText(1) # Multi-Value - accepts LYR files

# Work through all layers
    AllLyrs = Layers.split(";")
    for Layer in AllLyrs:
        arcpy.AddMessage("\nProcessing " + Layer)

        print Layer
        # Remove quotes from multi-value list
        if Layer[0] == "'":
            Layer = Layer[1:]
        if Layer[-1] == "'":
            Layer = Layer[:-1]
        print Layer

        # Process: Create Crowd MXD, Layer)

        del Layer
        print len(AllLyrs)

    del AllLyrs

except Exception, e:
  import traceback
  map(arcpy.AddError, traceback.format_exc().split("\n"))

The above code works in pyScripter if I feed it some values for the input parameters. It will not work in ArcCatalog, it gives a "IOError: [Errno 9] Bad file descriptor" error after 5 or 6 layers. If I use the "try...except" soultion recomended here ( it will work fine however.

If I remove the print statements, any of them, it will not work at all (in ArcCatalog or pyScripter), giving a "TypeError: object of type 'dict' has no len()" when reaching the "for Layer in AllLyrs" line.

In case you're wondering what the other tool called is, it is basically the "replace layer in mxd" example (, with some fixed values for the layer to replace. And that script works fine when run on its own using the same data and MXDs used in this script.

As I mentioned it works with the work around suggested for 9.3.1, but I can't see a reason for it to not work without the print statements???