0

I am creating a code that adds fields to a large set of shapefiles. I want to be able to open these shapefiles and edit the fields and then have them removed from the map document. I would like this because the amount of shapefiles that appear on the map document while the code is running is too large and ArcMap crashes.

Here is my code below:

for fc in arcpy.ListFeatureClasses():

    arcpy.AddField_management(fc, "C_TYPE", "TEXT") #add new field to each shapefile

    arcpy.AddField_management(fc, "C_UNIT", "TEXT") #add new field to each shapefile

    arcpy.AddField_management(fc, "INTERVAL", "LONG") #add new field to each shapefile

Below is what I want to remove:

enter image description here

Too many contours get accumulated in the document.

Is there any way I can add a piece of code that can remove this?

2
  • Do you have 64-bit background processing installed? This allows ArcMap to utilize more memory.
    – klewis
    Mar 13, 2019 at 18:00
  • 1
    What are the symptoms of "ArcMap crashes"? Do you get a Serious Application Error or something else?
    – PolyGeo
    Mar 13, 2019 at 20:01

1 Answer 1

2

You can prevent, that layers are added to the map with the following:

from arcpy import env
env.addOutputsToMap = 0

If you need the layers in the view, you can clear the view with the following:

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "")[0]
lyrlist = arcpy.mapping.ListLayers(mxd, "", df)

for lyr in lyrlist:
    arcpy.Delete_management(lyr)

Be aware that only deleting a layer-object does not delete the data. If you use arcpy.Delete_management() with a featureclass (e.g. from arcpy.ListFeatureClasses()) the data will be deleted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.