Trying to delete multiple fields in all shapefiles in a folder issue

4188
9
07-13-2012 10:05 AM
PeteJordan
Occasional Contributor III
I'm trying to modify a script where I can remove fields from all shapefiles in a specific folder.  The issue is I can only seem to do this for shapefiles I have specified in the gp.deletefield 'TEST.shp'.

  I don't want to specify a particular shapefile, I want this script to do this with a wildcard and all shapefiles in this folder.

  I am very new to Python, so I tried to use the wild card *.shp but that didn't do anything.

  So I researched and found the glob function.  Which I am having an issue trying to implement this into the script as
gp.deletefield glob.glob('*.shp', "Core; Comments")
of course will return an error.

So 2 questions are:
1) How can I get this script to remove the Core and Comment fields from all shapefiles in this folder?

2) Is there a way to make this script remove all *.shp files in all folders under the p:\\Traffic_Signals\KML\Jurisdictions rather than putting the script in each folder that contains a *.shp file?

  Thanks, I know this is pretty basic I am assuming, but just starting out here and needing to figure this out...


import arcpy
import arcgisscripting
import glob
gp = arcgisscripting.create()
gp.workspace = "p:\\Traffic_Signals\KML\Jurisdictions\TEST"
gp.toolbox = "management"

print "Removing Core and Comments from Shapefile"

try:
    # Delete fields
    gp.deletefield ('TEST.shp', "Core; Comments")
Tags (2)
0 Kudos
9 Replies
markdenil
Occasional Contributor III
You want to build a python list of the shape files and iterate through it.
Use the ListDatasets({wild_card}) tool to build the list.
That is where your '*.shp' wildcard goes.

You need to set the workspace before using ListDataSets (I see you did that...)

Then, your loop is like this:
import arcgisscripting
gp = arcgisscripting.create(9.3)
gp.workspace = "p:\Traffic_Signals\KML\Jurisdictions\TEST"
shapeList = gp.ListDatasets('*.shp') 
print "Removing Core and Comments from Shapefile"
for shape in shapeList:
    gp.DeleteField_management(shape, "Core; Comments")

OR
import arcpy
arcpy.env.workspace = "p:\Traffic_Signals\KML\Jurisdictions\TEST"
shapeList = arcpy.ListDatasets('*.shp') 
print "Removing Core and Comments from Shapefile"
for shape in shapeList:
    arcpy.DeleteField_management(shape, ["Core", "Comments"])


If you are using Arc10.x, then you can import arcpy and don't need arcgisscripting, although you can use it if you want.
If you are in arc9.x, you need arcgisscripting and cannot import arcpy
... and, if you are using arc9.3, use arcgisscripting(9.3)

-> Note the slightly different syntax for arcgisscripting and arcpy


for the second question...

you will have to search for the directories under your base one.
The os and os.path commands will help there.  (import os)
get a list of directory entries with os.listdir(path)
You can test for directories with os.path.isdir(path)

OR, you can hardcode the subdirectories and do the arcpy.ListDataSets for each one.
0 Kudos
ChristopherBlinn1
Occasional Contributor III
Have you tried to loop through your workspace using the ListFeatureClasses function?

import arcpy
from arcpy import env

env.workspace = "p:/Traffic_Signals/KML/Jurisdictions/TEST"
fcs = arcpy.ListFeatureClasses()

for fc in fcs:
     arcpy.DeleteField_management(fc, "Core; Comments")


Hope this helps!

Best,
Chris B.
0 Kudos
PeteJordan
Occasional Contributor III
Thanks the the input (yes I am using Arc 10).  The first one:

import arcpy
arcpy.env.workspace = "p:\Traffic_Signals\KML\Jurisdictions\TEST"
shapeList = arcpy.ListDatasets('*.shp') 
print "Removing Core and Comments from Shapefile"
for shape in shapeList:
    arcpy.DeleteField_management(shape, ["Core", "Comments"])


  Didn't do anything, but the second one

import arcpy
from arcpy import env

env.workspace = "p:/Traffic_Signals/KML/Jurisdictions/TEST"
fcs = arcpy.ListFeatureClasses()

for fc in fcs:
     arcpy.DeleteField_management(fc, "Core; Comments")


  Worked perfectly fine.  Not sure why that would be, but at least it's working.

  Now to try and test out the subfolders and see if the import os etc.  and see if I can get that to work.

Thanks for the suggestions...
0 Kudos
RDHarles
Occasional Contributor
Pete,
os.walk is what you seek.
This will delete fields from ALL shapefiles starting in the root of "p:/Traffic_Signals/KML/Jurisdictions/TEST"

import os, arcpy

start = "p:/Traffic_Signals/KML/Jurisdictions/TEST"

for root, dirs, files in os.walk(start):
    for file in files:        
        if file.endswith(".shp"):            
            path = os.path.abspath(os.path.join(root, file))
            print "Processing: "+path
            arcpy.DeleteField_management(path, "Core; Comments")

print "\nDone.\n"
0 Kudos
PeteJordan
Occasional Contributor III
Ahhhh thank you rdharles that worked perfectly...
0 Kudos
ChristopherThompson
Occasional Contributor III
The first one:

.... Didn't do anything, but the second one...

  Worked perfectly fine.  Not sure why that would be...


I think that first example didn't work because I don't believe ListDataSets works with shapefiles, it deals with feature datasets in a Geodatabase, or in a Coverage as well as other kinds of files.  If you look at the help for this shapefiles aren't indicated as being supported.
0 Kudos
nimitz
by
Occasional Contributor
Hi all,
I frequently bring in kml/km files and convert to GEODB's in ARCVIEW. I'm trying to modify this script in a way that I can place this script in a toolbox and run it after I've converted some (say a dozen) kml's for a project.

A) I've tried making scripts and storing them in a personal toolbox (eg Randy's Tools), but little success.
B) It seems you can modify this STANDALONE script easily for GEODB'S, but what would the OS.WALK function be used for?

thanks,

Randy
0 Kudos
BruceBacia
Occasional Contributor
Originally Posted by pjordan@drcog.org 
The first one:

.... Didn't do anything, but the second one...

Worked perfectly fine. Not sure why that would be...


The first one probably didn't work because the workspace file path was not declared properly.
0 Kudos
ChristopherThompson
Occasional Contributor III
The first one probably didn't work because the workspace file path was not declared properly.


Yeah, looks like that first had a double whammy going...  even if the workspace had been declared correctly, it still wouldn't have worked because ListDatasets doesn't return feature classes (or shapefiles) - only feature or raster datasets within a geodatabase and a few other things that are considered to be datasets and which typically live inside a geodatabase.
0 Kudos