1

I have a python script that is suppose to merge geodatabases. I have about 10 geodatabses, some of which have feature classes that are not in the others, everything else has the same schema. When I run the script, I get the following error:

Traceback (most recent call last):
  File "C:\Users\kahn8050\Desktop\PythonScripts\mergegeodatabases.py", line 29, in <module>
    lyrs = list_layers(gdb)
  File "C:\Users\kahn8050\Desktop\PythonScripts\mergegeodatabases.py", line 18, in list_layers
    for ds in arcpy.ListDatasets():
TypeError: 'NoneType' object is not iterable

I'm not exactly how to change the script so that it ignores NoneType and just merges the files that are the same. I have attached the script below:

import arcpy
import os
import time

START = time.time()

#### SET THESE VARIABLES BEFORE USING ####
model_path = (r"C:\Users\kahn8050\Desktop\PythonScripts\TEST\NFHL_04_20161108.gdb") # path to target geodatabase - schema should match that of input geodatabase(s)
paths_to_load = r'["C:\\Users\\kahn8050\\Desktop\\PythonScripts\\TEST\NFHL_06_20161110.gdb"]' # list of (str) paths to input geodatabase(s)

def list_layers(gdb):
    """
        Creates a list of feature classes in current workspace with full paths
        (including path to parent geodatabase). Returns list of layers.
    """
    print("\nGenerating layer list...")
    lyrs = [] 
    for ds in arcpy.ListDatasets():
        for fc in arcpy.ListFeatureClasses(feature_dataset = ds):
            fullpath = os.path.join(gdb, ds, fc)
            lyrs.append(fullpath)
    return lyrs

if __name__ == "__main__":
    for gdb in paths_to_load:
        arcpy.env.workspace = gdb

        print("***Beginning processing for {0}***".format(gdb))
        lyrs = list_layers(gdb)

        for lyr in lyrs:
            fc = os.path.basename(lyr)
            ds = os.path.basename(os.path.dirname(lyr))
                # if no dataset (i.e., fc is in root of geodatabase) then
                # ds = empty string
            target = os.path.join(model_path, ds, fc)
                # if ds = '' then join correctly ignores

            try:
            # append lyr to model_path/<ds>/<fc>
                arcpy.Append_management(lyr, target)
                print("Appended: {0}".format(target))
                with open ("appended_layers.txt", "a") as txt:
                    txt.write("{0}\n".format(lyr))
            except arcpy.ExecuteError as e:
                if "000732" in e:
                    pass
                    # target does not contain this feature class name
                    # TODO copy entire source layer to target?
                else:
                    # log failed attempts to append (e.g., non-matching schemas)
                    with open ("append_errors.txt", "a") as txt:
                        txt.write("***Failed to append {0}\n\t{1}\n".format(lyr, e))
                print(e)
                pass

    END = time.time()
    print("***Processing complete: time elapsed = {0} seconds***".format(END-START))
1

You are missing a slash \ in your paths_to_load

paths_to_load = r'["C:\\Users\\kahn8050\\Desktop\\PythonScripts\\TEST\NFHL_06_20161110.gdb"]' # list of (str) paths to input geodatabase(s)
                                                                      ^

Also you have for ds in arcpy.ListDataSets(), but this won't loop through anything if you don't have any feature datasets. This will result in the error 'NoneType' object is not iterable as your list contents will be NULL

Try this:

def list_layers(gdb):
    """
        Creates a list of feature classes in current workspace with full paths
        (including path to parent geodatabase). Returns list of layers.
    """
    print("\nGenerating layer list...")
    lyrs = [] 

    datasets = arcpy.ListDatasets()
    if datasets:
        for ds in datasets:
            for fc in arcpy.ListFeatureClasses(feature_dataset = ds):
                fullpath = os.path.join(gdb, ds, fc)
                lyrs.append(fullpath)

    fcs = arcpy.ListFeatureClasses()
    if fcs:
        for fc in arcpy.ListFeatureClasses():
            fullpath = os.path.join(gdb, fc)
            lyrs.append(fullpath)
    return lyrs
  • Thank you, that got rid of the error and seemed to have generated the layer list, but it is still not appending anything to the target gdb. – user3583235 Feb 10 '17 at 18:13
  • 1
    remove your try/except from your code and run again. try/except can get in the way when trying to debug what's happening. Once you have the script working you can put them back in to keep it tidy. When you remove them rerun the script and update your question with any error messages it returns. – Midavalo Feb 10 '17 at 18:41
  • I removed the try/except, and it still runs the same way, but no actual appending occurs. Here is the output: Generating layer list... Beginning processing for 6 Generating layer list... Beginning processing for 1 Generating layer list... Beginning processing for 1 Generating layer list... Beginning processing for 1 Generating layer list... Beginning processing for 0 Generating layer list... Processing complete: time elapsed = 5.55599999428 seconds – user3583235 Feb 10 '17 at 19:21
  • Please edit your question to include any new information – Midavalo Feb 10 '17 at 19:21
  • In once gdb I have several feature classes and in the other gdb I have feature classes with the exact same name and schema. Is the problem in the if__name__=="main"? – user3583235 Feb 10 '17 at 19:36

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.