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

2946
2
08-01-2011 06:23 AM
AndrewEgleton
New Contributor III
Hi,

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
try:
    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
        arcpy.gp.LCSCreateCrowdMXD(TempMXD, Layer)

        del Layer
        print len(AllLyrs)

    del AllLyrs

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


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 (http://support.esri.com/en/knowledgebase/techarticles/detail/35380) 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 (http://resources.arcgis.com/gallery/file/geoprocessing/details?entryID=A910AB18-1422-2418-3418-3885D...), 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???
Tags (2)
0 Kudos
2 Replies
JamesGraham
New Contributor III
Andrew, just curious if you'd figured this out already. 

Does the error occur when the user selects only one layer in the ParameterAsText(1)?  If you can make it through one or more iterations, does it die on the last one?
0 Kudos
JamieKass
Occasional Contributor
I have the same problem, and it seems to be pretty random when it occurs, which disturbs me as a programmer. Whenever this error occurs due to print statements left in my code from development (which is infrequent and hard to reproduce), the next iteration of running the tool works with no issue.
0 Kudos