I am making a python code for ArcGIS 9.3 :

-I have one folder with a lot of shapefile polygon inside - Ech polygon shapefile have a field "Average" and one ID - I have a table with each field corresponding to the ID of shapefile

I want to open each shapefile, take the "Average" and put it in the good field. So for shapefil 1, I take all the averages and i put them in line 1 of the table.

To do it I ha one SearchCursor in my shapefile and one UpdateCursor in my table. But it doesn't work and it seems to come from the update function.

The code is as following and the error come after "Marqueur 6.1" which is link to the SetValue method and to the update cursor.

The mesage error is : Marqueur 6.1 : ERROR 999999: Error executing function. Failed to execute (OpeTable2).

for infile in glob.glob(os.path.join(path, '*.shp')):

    MyShape = os.path.join(path, infile)
    Name = os.path.basename(MyShape)
    fileName, fileExtension = os.path.splitext(Name)


    if TestFieldCreate == False :
        TestFieldCreate = True
        rowsP = gp.UpdateCursor(MyShape)
        rowP = rowsP.Next()
        #row est une ligne et non une colonne
        while rowP:
            ID_BVs = rowP.GetValue(Field_ID_BV) 
            ID_BV_STR = str(ID_BVs)   #Converti en string
            NameBV = "BV"+ ID_BV_STR
            rowP = rowsP.Next()
            gp.addfield (MyTable, NameBV, "double")

        #On place le curseur dans le shp

    rowsInShape = gp.SearchCursor(MyShape)
    rowInShape = rowsInShape.Next()

    while rowInShape:

        countStr =str(count)
        gp.AddMessage( "Iteration N°" + countStr)
        Moyenne = rowInShape.GetValue(Field_Moyenne)

        if Moyenne != 0 :
            gp.AddMessage("Marqueur 6")

            # On récupère les valeur pour AfoisP et le num de BV que l'on converti de la même manière que le nom des champs 
            BV = rowInShape.GetValue(Field_ID_BV) 
            BV_STR = str(BV)   #Converti en string
            FieldName = "BV"+ BV_STR
            gp.AddMessage("Marqueur 6.1")
            rowInTable.SetValue(FieldName, Moyenne)
            rowInTable.SetValue("NomFich", fileName)
            gp.AddMessage("Marqueur 7")

        gp.AddMessage("Marqueur 7.1")
        rowInSHape = rowsInShape.Next()

    gp.AddMessage("Marqueur 7.2")
    rowInTable = rowsInTable.Next()
    gp.AddMessage("Marqueur 8")
    NumStr = str(Num)
    gp.AddMessage(Name + " Fichier N° " + NumStr)
    Num = Num +1
    gp.AddMessage("Marqueur 9")

Thanks a lot

  • It looks like you are generating the field name to store the value in based on some value in the input shape file. Do the fields actually exist? I don't see them being created anywhere. Also, you should verify that you aren't getting any whitespace in that field name as well. – Evil Genius Mar 31 '14 at 11:17

I think the error is being caused because you are altering the schema of the output table after the InsertCursor is already open.

Try splitting your script into two different loops: one that adds the fields to your output table (sorry I missed that part previously), create your InsertCursor and then a final loop that calculates the values and appends them to the table.
It's been a while since I've done much with 9.3.1, but I do remember some odd things happening if the schema was altered while a cursor is open.

  • You are right ! I didn't delete the cursor : rowsP before to use the cursor rowsInShape. So I need to have twice the for loop, in order to delete the first cursor. Thanks ! ;) – Panvjim0 Mar 31 '14 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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