By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,814 Members | 1,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,814 IT Pros & Developers. It's quick & easy.

How to remove directory from file name

P: 2
I am new to python converting from C++ so this may seems like a simple solution. I have a script that when a drive and folder name is input, a field is added to all shapefiles within the folder. It then adds the drive name and folder name to the field. I need just the folder name added to the field without the drive letter. The next question is, I want to modify the code to read from a geodatabase instead of a shapefile. Thanks for any suggestions. Here is the code:


Expand|Select|Wrap|Line Numbers
  1. import sys, string, os, arcgisscripting
  2.  
  3. gp = arcgisscripting.create()
  4.  
  5. # this is the directory user must specify
  6. gp.workspace = sys.argv[1]
  7. # declare the given workspace so we can use it in the update field process
  8. direct = gp.workspace
  9. try:
  10.     fcs = gp.ListFeatureClasses("*", "all")
  11.     fcs.reset()
  12.     fc = fcs.Next()
  13.  
  14.  
  15.     while fc:
  16.         fields = gp.ListFields(fc, "Airport")
  17.         field_found = fields.Next()
  18.         # check if the field allready exist.
  19.         if field_found:
  20.             gp.AddMessage("Field %s found in %s and i am going to delete it" % ("Airport", fc))
  21.             # delete the "SHP_DIR" field
  22.             gp.DeleteField_management(fc, "Airport")
  23.             gp.AddMessage("Field %s deleted from %s" % ("Airport", fc))
  24.             # add it back
  25.             gp.AddField_management (fc, "Airport", "text", "", "", "50")
  26.             gp.AddMessage("Field %s added to %s" % ("Airport", fc))
  27.             # calculate the field passing the directory and the filename
  28.             gp.CalculateField_management (fc, "Airport", '"' + direct + '"')
  29.             fc = fcs.Next()
  30.  
  31.  
  32.         else:
  33.             gp.addMessage(" layer %s has been found and there is no Airport" % (fc))
  34.         # Create the new field
  35.             gp.AddField_management (fc, "Airport", "text", "", "", "50")
  36.             gp.AddMessage("Field %s added to %s" % ("Airport", fc))
  37.  
  38.         # Apply the folder to all entries
  39.             gp.CalculateField_management(fc)
  40.             fc = fcs.Next()
  41.         gp.AddMessage("field has been added succesfully")
  42.         # Remove directory
  43.  
  44. except:
  45.     mes = gp.GetMessages ()
  46.     gp.AddMessage(mes)
Sep 15 '10 #1
Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Module os.path has functions to manipulate pathnames. Example:
Expand|Select|Wrap|Line Numbers
  1. >>> import os
  2. >>> os.path.splitdrive("C:\\temp")
  3. ('C:', '\\temp')
  4. >>> 
I am unfamiliar with shapefiles and geodatabases, so I don't know how to answer question #2.
Sep 15 '10 #2

P: 2
Thanks a lot
Sep 15 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.