How to set 2 workspace environments?

687
2
05-14-2012 09:04 AM
MikeMacRae
Occasional Contributor III
I was writing a code to compare the fields within some feature classes that are in 2 seperate geodatabases. The geodatabses contain the same feature classes, but the feature classes have different fields.


  1. Loop through first geodatabase

  2. Loop through second geaodatabase and find the matching feature class

  3. Loop through the matching feature classes and print the list of fields for each into a spreadsheet


My following code wasn't working, and I narrowed it down to the loop through the second feature class. I went back and the syntax requires that a workspace be set to list feature classes. I did that using
env.workspace
but that only sets a workspace for the first geodatabase. I tried setting variables to 2 different workspaces (workspace1 and workspace2) but that doesn't seem to work. Can someone suggest how to set seperate workspaces so I can loop through 2 different geodatabases in the same script?

import arcpy, xlwt, traceback, sys
from arcpy import env
from xlwt import *

workspace1 = env.workspace = r"Z:\test1.gdb"
workspace2 = env.workspace = r"Z:\test2.gdb"
try:
    # Create the excel workbook
    book = Workbook()

    z = 2
    x = 2
    y = 1
    for fc in arcpy.ListFeatureClasses(workspace1):
        print fc
        for fc2 in arcpy.ListFeatureClasses(workspace2):
            print fc2
                if fc == fc2:

                    print fc + " = " + fc2
                    
                    sheet1 = book.add_sheet(fc)
                    sheet1.write(0,0, fc)
                    sheet1.write(1,0, "IOR Field Name")
                    sheet1.write(1,1, "IOR Field Type")
                    sheet1.write(1,2, "Field Length")

                    sheet1.write(1,5, "JC Field Name")
                    sheet1.write(1,6, "JC Field Type")
                    sheet1.write(1,7, "JC Field Length")            

                    sheet1.col(0).width = 8000
                    sheet1.col(1).width = 3000
                    sheet1.col(2).width = 3000
                    sheet1.col(5).width = 8000
                    sheet1.col(6).width = 3000
                    sheet1.col(7).width = 3000
                    
                    y += 1
                    
                    for field in arcpy.ListFields(fc):
                        x+=1
                        sheet1.write(x,0, field.name)
                        sheet1.write(x,1, field.type)
                        sheet1.write(x,2, field.length)
                        
                    for field2 in arcpy.ListFields(fc2):
                        z+=1
                        sheet1.write(z,5, field.name)
                        sheet1.write(z,6, field.type)
                        sheet1.write(z,7, field.length)

                book.save(r"Z:" + "\\" + "Template_Geodatabase_FieldList2" + ".xls")

Tags (2)
0 Kudos
2 Replies
StephenKruzik
New Contributor III
had a script using the 'env', and I found it caused errors for me if I didn't have the 'arcpy.' before it.  So my first suggestion would be to try making it 'arcpy.env.workspace' instead of just 'env.workspace'.

In the python debugger, it comes up with the variable being the 2 different paths, so hopefully that is what the issue was.  If not, then I'm not really sure myself.
0 Kudos
by Anonymous User
Not applicable
I'm not sure this is the best solution, but the following appears to work for me.

import arcpy, xlwt, traceback, sys
from arcpy import env
from xlwt import *

workspace1 = r"D:\work\python\test2.gdb"
workspace2  = r"D:\work\python\test1.gdb"
try:
    # Create the excel workbook
    book = Workbook()
    y = 1

    env.workspace = workspace1
    fclist1 = arcpy.ListFeatureClasses()
    print fclist1

    env.workspace = workspace2
    fclist2 = arcpy.ListFeatureClasses()
    print fclist2
    
    for fc in fclist1:
        print fc
        z = 2
        x = 2
        if fc in fclist2:
            print "found: " + fc
            sheet1 = book.add_sheet(fc)
            sheet1.write(0,0, fc)
            sheet1.write(1,0, "IOR Field Name")
            sheet1.write(1,1, "IOR Field Type")
            sheet1.write(1,2, "Field Length")
            sheet1.write(1,5, "JC Field Name")
            sheet1.write(1,6, "JC Field Type")
            sheet1.write(1,7, "JC Field Length")            
            sheet1.col(0).width = 8000
            sheet1.col(1).width = 3000
            sheet1.col(2).width = 3000
            sheet1.col(5).width = 8000
            sheet1.col(6).width = 3000
            sheet1.col(7).width = 3000
            
            y += 1
            env.workspace = workspace1
            for field in arcpy.ListFields(fc):
                x+=1
                sheet1.write(x,0, field.name)
                sheet1.write(x,1, field.type)
                sheet1.write(x,2, field.length)
               
            env.workspace = workspace2
            for field2 in arcpy.ListFields(fc):
                z+=1
                sheet1.write(z,5, field2.name)
                sheet1.write(z,6, field2.type)
                sheet1.write(z,7, field2.length)
        else:
            print "didn't find: " + fc
                
    book.save(r"Template_Geodatabase_FieldList1.xls")
except:
    print "error"


John O
0 Kudos