[GIS] Unsupported operand error writing to file (Python, arcgis)

arcgis-desktoppython

In the code below, I get a "TypeError: unsupported operand type(s) for +: 'NoneType and 'str'" on the f.write line. If I don't concatenate, it runs fine, but all the layer names run together.

I understand what the error means, but not why I'm getting a NoneType. I've tried casting lyr.name to str, same result. All I'm trying to do is write out a text file of layer names. Any idea what's causing this and how to get around it? I've tried testing for type is None, isinstance(None), no luck.Thanks.

    f = open(r"C:\ListOfLayers.txt", "w")
    count = 0
    lyrs = arcpy.mapping.ListLayers(mxd, "", df)

    for lyr in lyrs:
        f.write(str(s)) + "\n"
        count += 1

    f.write("\n \n There are " + str(count) + " layers.")

    f.close()

Best Answer

In the line f.write(str(s)) + "\n", I think you want this instead: f.write(str(s) + "\n")