1

I have different month folders, each folder contain a shapefile called, For January folder Merged001.shp, For FEBRUARY Merged002.shp so on,

That shapefile having one field name 'Solar', and i am trying to delete specific value from Solar field or we can say whichever value < 15 its will be deleted from Solar field (which is < 15.)

I have made a script to loop through all the month folder and read it the shapefile's Solar field and delete it < 15 value.

Below is my script and error massage

    import arcpy, os, calendar
    from arcpy import env
    from arcpy.sa import *
    arcpy.CheckOutExtension("Spatial")
    topWorkspace = r'D:\Delete_Test'
    env.workspace = topWorkspace
    env.overwriteOutput = True

    months = {calendar.month_name[i].upper(): str(i).zfill(3) for i in range(1, 13)} 
    for folderPath in arcpy.ListWorkspaces():
        baseName = os.path.basename(folderPath).upper()
        if baseName in months: 
            monthNumber = months[baseName]
        arcpy.env.workspace = folderPath

        shapeFiles = (arcpy.ListFeatureClasses('Merged_*.shp')[0])
        print shapeFiles

    with arcpy.UpdateCursor(shapeFiles, ["Solar"]) as cursor:
 for row in cursor:
    if row [0] < 17:
        cursor.deleteRow(row)
del row

error massage :

Traceback (most recent call last):
  File "<module2>", line 19, in <module>
AttributeError: __exit__
6
  • 1
    Just want to verify your indentation is correct on your question: rows = line is 'outside' the for folderPath block? Commented Mar 13, 2017 at 17:00
  • i have to put folderpath there instead of ShapeFiles ?
    – SWAT
    Commented Mar 13, 2017 at 17:30
  • I have updated the below line, but problem still persist with arcpy.UpdateCursor(shapeFiles, ["Solar"]) as cur: for row in cur: if row.Solar < 17: rows.deleteRow(row) del row
    – SWAT
    Commented Mar 13, 2017 at 17:48
  • It is usually easier if you just update your question with any changes in formatting/code you've done. Pay close attention to your indenting. Then other people have a better chance of understanding your problem. Commented Mar 13, 2017 at 17:57
  • I didn't update anything on source code, and my question remain same at the end, whatever i tried i mentioned in comment line
    – SWAT
    Commented Mar 13, 2017 at 18:06

1 Answer 1

1

First thing I would check is your indentation.

for folderPath in arcpy.ListWorkspaces():
    baseName = os.path.basename(folderPath).upper()
    if baseName in months: 
        monthNumber = months[baseName]
    arcpy.env.workspace = folderPath

    shapeFiles = (arcpy.ListFeatureClasses('Merged_*.shp')[0])
    print shapeFiles

rows = arcpy.UpdateCursor(shapeFiles, "", "", "Solar")

The way this code reads is that you go through all the workspaces and assign the feature classes to the shapeFiles tuple. You just then print the shapeFiles.

Since the rows = line is not indented, it is outside the loop. It will only run against the last record in your loop. This is probably not what you want.

Try:

for folderPath in arcpy.ListWorkspaces():
    baseName = os.path.basename(folderPath).upper()
    if baseName in months: 
        monthNumber = months[baseName]
    arcpy.env.workspace = folderPath

    shapeFiles = (arcpy.ListFeatureClasses('Merged_*.shp')[0])
    print shapeFiles

    rows = arcpy.UpdateCursor(shapeFiles, "", "", "Solar")
    for row in rows:
        if row.Solar < 17:
            rows.deleteRow(row)
    del row
    del rows

The next thing that looks odd to me is that you are asking the row for its value of Solar using row.Solar. Typically, to get the value from the row, you use an index to read the tuple:

if row[0] < 17:
    rows.deleteRow(row)

The value of row[0] refers to the 'Solar' column since it's the first (and only) field listed.

Also, and this is just extra, when dealing with cursors, use the arcpy.da module instead. They are much speedier. The syntax is slightly different, so be careful:

with arcpy.da.UpdateCursor(shapeFiles, "Solar") as cursor:
    for row in cursor:
        if row[0] < 17:
            cursor.deleteRow()

There may be other issues with your code, but if you are seeing the correct value of shapeFiles being printed out, you should be good.

0

Your Answer

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

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