views:

68

answers:

1

I am a python newbie and have obtained a script that lets the user input a directory where shapefiles are located (ex., c:\programfiles\shapefiles). It then creates a field within each shapefile and adds the directory path that was input and the shapefile name (ex., c:\programfiles\shapefiles\name.shp). I would like to populate the field with just the directory name (ex., shapefiles). I know there is a command that will split the directory name, but how do I return the basename as a function ? Thanks in advance.

import sys, string, os, arcgisscripting
gp = arcgisscripting.create()

# this is the directory user must specify
gp.workspace = sys.argv[1]
# declare the given workspace so we can use it in the update field process
direct = gp.workspace
try:
    fcs = gp.ListFeatureClasses("*", "all")
    fcs.reset()
    fc = fcs.Next()


    while fc:
        fields = gp.ListFields(fc, "Airport")
        field_found = fields.Next()
        # check if the field allready exist.
        if field_found:
            gp.AddMessage("Field %s found in %s and i am going to delete it" % ("Airport", fc))
            # delete the "SHP_DIR" field
            gp.DeleteField_management(fc, "Airport")
            gp.AddMessage("Field %s deleted from %s" % ("Airport", fc))
            # add it back
            gp.AddField_management (fc, "Airport", "text", "", "", "50")
            gp.AddMessage("Field %s added to %s" % ("Airport", fc))
            # calculate the field passing the directory and the filename
            gp.CalculateField_management (fc, "Airport", '"' + direct + '\\' + fc + '"')
            fc = fcs.Next()


        else:
            gp.addMessage(" layer %s has been found and there is no Airport" % (fc))
        # Create the new field
            gp.AddField_management (fc, "Airport", "text", "", "", "50")
            gp.AddMessage("Field %s added to %s" % ("Airport", fc))

        # Apply the directory and filename to all entries       
            gp.CalculateField_management (fc, "Airport", '"' + direct + '\\' + fc + '"')
            fc = fcs.Next()
        gp.AddMessage("field has been added successfully")
        # Remove directory

except:
 mes = gp.GetMessages ()
 gp.AddMessage(mes)
+2  A: 

http://docs.python.org/library/os.path.html#os.path.dirname

os.path.dirname(your_full_filename)
WoLpH