I'm trying to write a script to copy multiple feature classes from two different databases into a single database, but I want to be able to specity what features I actually want. I know I need to use da.Walk to get through the first files but am stumped on how I can actually do this. Any thoughts?

here is what i have so far.

`import arcpy, os, string 

workspace= ""

for dirpath, dirnames, filenames in arcpy.da.Walk(workspace, datatype="FeatureClass"):
    for filename in filenames:`
  • 1
    If you create a layer with a whereclause then use a tool like append then only the specified features will be copied. What part are you having trouble with? Can you post the code you've got please. – Michael Stimson Feb 4 '15 at 1:15
  • 1
    please edit your question and insert your code with formatting. It is very important for python to be formatted correctly (use the {} button) – Michael Stimson Feb 4 '15 at 1:31
  • Edited the code formatting. – Ddogh Feb 4 '15 at 1:48

The following is an example of how you can use a list comprehension, fnmatch and walk to get at specific files within multiple geodatabases. In this example I specified that I wanted all polygon feature classes with a "poly" prefix. As an added bonus, list comprehensions are blazingly fast compared to stand alone nested for loops.

import arcpy, os, fnmatch

path = r'C:\gdrive\temp\test'

myfiles = [os.path.join(dirpath, f)
    for dirpath, dirnames, files in arcpy.da.Walk(path, datatype="FeatureClass", type="Polygon")
    for f in fnmatch.filter(files, 'poly*')]

# Copy the files over starting here...

enter image description here

  • That's very similar to os.walk() - another handy one to know. Is fnmatch a standard library, I've not seen that one before? Is the extra ']' supposed to be there on the end of for f in fnmatch.filter(files, 'poly')]*? – Michael Stimson Feb 4 '15 at 2:07
  • @Michael Miles-Stimson Yes, fnmatch is a standard library with Python 2.7. The ] is supposed to be there--it is part of the list comprehension. – Aaron Feb 4 '15 at 3:48
  • I see it now, it's on 3 lines. Thanks @Aaron. – Michael Stimson Feb 4 '15 at 4:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.