Select to view content in your preferred language

del cur error ????

5323
7
Jump to solution
02-06-2015 02:24 PM
PROBERT68
Honored Contributor

This is what I wrote from the book Programming ArcGIS 10.1 with Python Cookbook wrote by Eric Pimper and it is on Chapter 7th.

 

 

 

#Script to Import data to a feature class within a geodatabase

import arcpy, os

arcpy.env.workspace = "C:/ArcpyBook/data/Wildfires/WildlandFires.mdb"

f = open("C:/ArcpyBook/data/Wildfires/NorthAmericaWildfires_2007275.txt", "r")

 

 

try:

    # the output feature class name

    outputFC = arcpy.GetParameterAsText(0)

    # the template feature class that defines the attribute schema

    fClassTemplate = arcpy.GetParameterAsText(1)

    #Open the file to read

    f = open(arcpy.GetParameterAsText(2), 'r')

    arcpy.CreateFeatureclass_management(os.path.split(outputFC)[0], os.path(outputFC)[1]), "point", (fClassTemplate)

    lstFires = f.readlines()

    cur = arcpy.InsertCursor(outputFC)

    cntr = 1

    for fire in lstFires:

        if 'Latitude' in fire:

            continue

        vals = fire.split(",")

        latitude = float(vals[0])

        longitude = float(vals[1])

        confid = int(vals[2])

        pnt = arcpy.Point(longitude, latitude)

        feat = cur.newRow()

        feat.shape = pnt

        feat.setValue("CONFIDENCEVALUE", confid)

        cur.insertRow(feat)

        arcpy.AddMessage("Record number" + str(cntr) + "written to feature class")

        cntr = cntr + 1

except:

    print arcpy.GetMessages()

finally:

    del cur

    f.close()

 

The error showed here

 

Traceback (most recent call last):

  File "C:\ArcpyBook\Ch7\InsertWildfires.py", line 34, in <module>

    del cur

NameError: name 'cur' is not defined

0 Kudos
1 Solution

Accepted Solutions
PROBERT68
Honored Contributor

Okay I finally figured it out !  Apparently the book on Chapter 7 has some errors in it as I was trying to work on them in Pyscripter and Python Window and got it worked ! This HAS nothing to do with the del cur I had to re-read the instructions and found that the chapter mention to delete two hardcoded THAT should be include in the python script !    phew !

This is the correct coding.

#Script to Import data to a feature class within a geodatabase

import arcpy, os

arcpy.env.workspace = "C:/ArcpyBook/data/Wildfires/WildlandFires.mdb"

f = open("C:/ArcpyBook/data/Wildfires/NorthAmericaWildfires_2007275.txt", "r")

cur = arcpy.InsertCursor("FireIncidents")

try:

    # the output feature class name

    outputFC = arcpy.GetParameterAsText(0)

    # the template feature class that defines the attribute schema

    fClassTemplate = arcpy.GetParameterAsText(1)

    #Open the file to read

    f = open(arcpy.GetParameterAsText(2), 'r')

    arcpy.CreateFeatureclass_management(os.path.split(outputFC)[0],os.path.split(outputFC)[1],"POINT", fClassTemplate)

    lstFires = f.readlines()

    cur = arcpy.InsertCursor(outputFC)

    cntr = 1

    for fire in lstFires:

        if 'Latitude' in fire:

            continue

        vals = fire.split(",")

        latitude = float(vals[0])

        longitude = float(vals[1])

        confid = int(vals[2])

        pnt = arcpy.Point(longitude, latitude)

        feat = cur.newRow()

        feat.shape = pnt

        feat.setValue("CONFIDENCEVALUE", confid)

        cur.insertRow(feat)

        arcpy.AddMessage("Record number " + str(cntr) + "written to feature class")

        cntr = cntr + 1

except:

    print arcpy.GetMessages()

finally:

    del cur

    f.close()

View solution in original post

0 Kudos
7 Replies
DanPatterson_Retired
MVP Emeritus

I suspect it is already gone... whenever you run a script through an IDE, you can inspect its variables using

locals().keys()     (or globals or vars .keys() )

to see what namespace remains after a script is run and what has been delete is what is in your script and these values

0 Kudos
PROBERT68
Honored Contributor

Where do I find this information in an IDE ? Would Pyscripter show the avaiable variable to replace the cur ?

0 Kudos
DanPatterson_Retired
MVP Emeritus

you can run the samples I posted in PyScripter in the interactive section

0 Kudos
PROBERT68
Honored Contributor

Thank you. That is what I thought so. Does that mean ESRI took out the cur sytanx ? Ok, I will look into that as what you suggest. I have googled it as well but did not find it. I tried cursor and it came back an error...

0 Kudos
DanPatterson_Retired
MVP Emeritus

Here is an example....  before a script is run...a script is run with no namespace deletion...then the namespace that remains after

You can get real fancy and do set differences etc etc if you want.

>>> a = set(locals().keys())
>>> a = list(set(locals().keys()))
>>>
>>> a = list(set(locals().keys()))
>>> a.sort()
>>> a
['__builtins__', '__doc__', '__name__', '__package__', 'a', 'pywin']
>>> 
>>> # now run a script....
>>> 
>>> 
Running...C:\!Scripts\Cursors\cursor_functions.py
Spatial Reference NAD_1983_CSRS_MTM_9
>>> 
>>> b = list(set(locals().keys()))
>>> b.sort()
>>> 
>>> a
['__builtins__', '__doc__', '__name__', '__package__', 'a', 'pywin']
>>> b
['SR', 'SR_PCSCode', '__builtins__', '__doc__', '__name__', '__package__', 'a', 'arcpy', 'desc', 'find_overlaps', 'input_shp', 'move', 'os', 'poly_pnts', 'pywin', 'sys']
>>> 
>>>
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Robert, if you could use Syntax highlighting in the advanced editor, it would make reading and discussing your code easier for commenters.

You are getting that error because of the del cur statement in the finally section of your code.  In Python error handling, the finally clause is always executed before exiting the try statement, even if there was an error.  In your case, you had an error either while creating the cursor object or before, and the cursor object was never instantiated.  Since it wasn't instantiated, it can't be deleted in the finally clause.

Looking at the code, you should have received another error message before the del cur error.  What other messages did you receive?

0 Kudos
PROBERT68
Honored Contributor

Okay I finally figured it out !  Apparently the book on Chapter 7 has some errors in it as I was trying to work on them in Pyscripter and Python Window and got it worked ! This HAS nothing to do with the del cur I had to re-read the instructions and found that the chapter mention to delete two hardcoded THAT should be include in the python script !    phew !

This is the correct coding.

#Script to Import data to a feature class within a geodatabase

import arcpy, os

arcpy.env.workspace = "C:/ArcpyBook/data/Wildfires/WildlandFires.mdb"

f = open("C:/ArcpyBook/data/Wildfires/NorthAmericaWildfires_2007275.txt", "r")

cur = arcpy.InsertCursor("FireIncidents")

try:

    # the output feature class name

    outputFC = arcpy.GetParameterAsText(0)

    # the template feature class that defines the attribute schema

    fClassTemplate = arcpy.GetParameterAsText(1)

    #Open the file to read

    f = open(arcpy.GetParameterAsText(2), 'r')

    arcpy.CreateFeatureclass_management(os.path.split(outputFC)[0],os.path.split(outputFC)[1],"POINT", fClassTemplate)

    lstFires = f.readlines()

    cur = arcpy.InsertCursor(outputFC)

    cntr = 1

    for fire in lstFires:

        if 'Latitude' in fire:

            continue

        vals = fire.split(",")

        latitude = float(vals[0])

        longitude = float(vals[1])

        confid = int(vals[2])

        pnt = arcpy.Point(longitude, latitude)

        feat = cur.newRow()

        feat.shape = pnt

        feat.setValue("CONFIDENCEVALUE", confid)

        cur.insertRow(feat)

        arcpy.AddMessage("Record number " + str(cntr) + "written to feature class")

        cntr = cntr + 1

except:

    print arcpy.GetMessages()

finally:

    del cur

    f.close()

0 Kudos