Select to view content in your preferred language

Another simple question - close feature class or shapefile without closing script

812
4
04-05-2011 09:32 AM
TerrySilveus
Frequent Contributor
I have the following script (partial shown)
... So really I have two questions -
1) how do I close the shapefile without having to exit all the way out of pythonWin?
2) why is my code not working...too general huh... I check to see if a field exists and if it doesn't, I add it - that part works fine... next I try to set up a search cursor based on a where clause so that I can update another cursor - my search cursor never gets loaded so there is nothing for the update cursor to work off of.... Basically, I don't have arcInfo level license so I'm trying to recreate the joinField function from arcInfo at the arcView license level.  I'm an experienced programmer, but not familiar with either python or arcpy.

    #check to see if it has the field that will be updated, if it has it good; if not then add the field
    if not updateFieldParam in updateTheFields:
        #field does not exist - need to add field
       arcpy.AddField_management(updateDSParam,updateFieldParam,fieldTypeDictionary[fldType], fldPrecision, fldScale, fldLength) #adds field to data set

    updateTheRows = arcpy.UpdateCursor(updateDSParam) #get the rows from the shapefile that will have a field updated
    for updateTheRow in updateTheRows:
        #loop through all the rows in the update cursor
        if linkTypeText:
            whereClause = "!" + searchLinkParam + "! = '" + updateTheRow.getValue(updateLinkParam) + "'"
        else:
            whereClause = "!" + searchLinkParam + "! = " + updateTheRow.getValue(updateLinkParam)
           
        searchTheRows = arcpy.SearchCursor(searchDSParam,whereClause)
        for searchTheRow in searchTheRows:
            if searchTheRow.getValue(searchFieldParam) <> "":
                updateTheRow.setValue(updateFieldParam,searchTheRow.getValue(searchFieldParam))
                updateTheRows.updateRow(updateTheRow)
                break #exit search cursor loop - you found first row with data and updated row
           
        searchTheRows = None
    updateTheRows = None





EDITED FOR ADDITIONAL INFORMATION:
My indents are correct... they just didn't translate correctly on the forum
the "search cursor" comes from a table in a geodatabase not a shapefile
the "update cursor" is a feature class in a geodatabase
Tags (2)
0 Kudos
4 Replies
MarkWiygul
Emerging Contributor
I might be able to offer some assitance with this one:

"  1) how do I close the shapefile without having to exit all the way out of pythonWin?  "

I think you are looking for the 'del' keyword in python.  the python 'help()' and 'dir()' and 'type()' functions can advise you of every object you'd like to know about but doesn't offer any assitance on keywords.

try this line to close the shapefile (without typing the chevron in)
>>>del updateTheRows

if that doesn't work type this
>>>help(updateTheRows)


[it works with arcgisscripting; I guess with arcpy too unless an expert here suggests differently]
good luck!
0 Kudos
TerrySilveus
Frequent Contributor
I have a whole string of del(s) for all my variables/objects at the end of processing... but if I leave PythonWin open, I can't get exclusive access in arcGIS even after the script finishes and all the del(s) have been executed.

finally:
    #clean up everything
    if searchDSParam:
        del searchDSParam

    if searchLinkParam:
        del searchLinkParam

    if updateDSParam:
        del updateDSParam

    if searchFieldParam:
        del searchFieldParam

    if updateLinkParam:
        del updateLinkParam

    if updateFieldParam:
        del updateFieldParam

    if updateTheRow:
        del updateTheRow

    if updateTheRows:
        del updateTheRows

    if searchTheRow:
        del searchTheRow

    if searchTheRows:
        del searchTheRows

    if updateTheFields:
        del updateTheFields

    if searchTheFields:
        del searchTheFields

    if searchTheField:
        del searchTheField

    if fldLength:
        del fldLength

    if fldType:
        del fldType

    if fldPrecision:
        del fldPrecision

    if fldScale:
        del fldScale

    if whereClause:
        del whereClause

    if linkTypeText:
        del linkTypeText

    if fieldTypeDictionary:
        del fieldTypeDictionary
0 Kudos
MarkWiygul
Emerging Contributor
del should relinquish control of any variable that is holding write access. 

I hope someone with experience explains what's happening here.

You can type your code in between the following tags:
 

EXCEPT that with the final tag us a "/" inbetween the "[" and "CODE]" (I can't type it out for obvious reasons ...I'd get a code block :) like so...
you can also just hit the "#" button 
above the message box to get the code block
0 Kudos
TerrySilveus
Frequent Contributor
Thanks for the code formatting tip


Still waiting on resolution from issues....
I think issue #2 may be related to the data being a table in a geodatabase and not a feature class, but if so, how can I load a cursor with that data?
EDITED ADDITIONAL INFORMATION: - I even took away the whereClause so it should have loaded all rows, but still none went into the cursor. So I have narrowed it down to
searchTheRows = arcpy.SearchCursor(searchDSParam,whereClause)
as the problem.(But no errors)  The other cursor loads just fine.

As for issue 1 - don't know why del is not releasing the files (I stepped right through the statements), but when I close PythonWin the files are released and I can regain exclusive access in arcGIS.
0 Kudos