natogd

Why does one script work but not the other? arcpy.Merge_management

Discussion created by natogd on Mar 15, 2011
Latest reply on Mar 15, 2011 by natogd
I am trying to merge 90 tables in separate access databases (not spatial) into a single file GDB or SDE table.  This is a task I perform regularly.  My first script does not perform the task.  My second merges the files correctly.  I'm just a copy and paste coder so I am not able to figure out why one works and the other doesn't.

### First Script which does not work
# import system modules
import os, arcpy, datetime
#from arcpy import env

# Obtain list of DWG Files to import
# This list is automatically generated from the permanent AltaLIS DWG folder
out_folder_path = "\\\\myserver\\gis\\DATA\\Permanent\\AltaLIS\\MDB2"
out_name = "Merged.gdb"
out_GDB = out_folder_path + "\\" + out_name
##out_Basic = "\\" + "Basic"
#tableList = ['Basic', 'Linc']

# Beginning the Main part of the script
now1 = datetime.datetime.now()
print 'Beginning Script ' + str(now1.hour) + ":" + str(now1.minute) + ":" + str(now1.second)
print out_GDB
# Stopping ArcGIS Server Object Manager on myserver to unlock files
print '1 Taking Server7 ArcGIS Server Object Manager temporarily offline.'
try:
    os.system('sc \\\\myserver stop ArcServerObjectManager')
except:
    print 'Could not stop ArcServerObjectManager'
arcpy.geoprocessing.gp.setProduct("arcinfo")
arcpy.env.workspace = out_folder_path
if arcpy.gp.exists(out_folder_path + "\\" + out_name):
    arcpy.Delete_management(out_folder_path + "\\" + out_name)
    arcpy.CreateFileGDB_management(out_folder_path, out_name)
    print "The fGDB already exists"
else:
    print "The fGDB is being created"
    arcpy.CreateFileGDB_management(out_folder_path, out_name)

datasetList = arcpy.ListWorkspaces("*.mdb", "All")
L = []
try:
    for dataset in datasetList:
        #print dataset
        L.append(dataset)
        print '  - ' + str(dataset)
except:
    print "Error."

# Updates basic Table in file GDB
out_basic = "\\" + "basic"
if arcpy.gp.exists(out_GDB + out_basic,""):
    arcpy.Delete_management(out_GDB + out_basic,"")
    print '2A ' + out_GDB + out_basic + ' deleted successfully.'
else:
    print '2B ' + out_GDB + out_basic + ' doesn\'t exist.'

try:
    BasicMerged = out_GDB + out_basic
    arcpy.Merge_management(L,BasicMerged)
    print "Merge successful: " + out_basic
except:
    print "Merge failed: " + out_basic

os.system('sc \\\\myserver start ArcServerObjectManager')

now2 = datetime.datetime.now()
print 'Ending Script ' + str(now2.hour) + ":" + str(now2.minute) + ":" + str(now2.second)


### Second Script that works
# import system modules
import os, arcpy, datetime
#from arcpy import env

# Obtain list of DWG Files to import
# This list is automatically generated from the permanent AltaLIS DWG folder
out_folder_path = "\\\\myserver\\gis\\DATA\\Permanent\\AltaLIS\\MDB2"
out_name = "Merged.gdb"
out_GDB = out_folder_path + "\\" + out_name
out_Basic = "\\" + "Basic"
#tableList = ['Basic', 'Linc']


# Beginning the Main part of the script
now1 = datetime.datetime.now()
print 'Beginning Script ' + str(now1.hour) + ":" + str(now1.minute) + ":" + str(now1.second)
print out_GDB

# Stopping ArcGIS Server Object Manager on myserver to unlock files
print '1 Taking Server7 ArcGIS Server Object Manager temporarily offline.'
try:
    os.system('sc \\\\myserver stop ArcServerObjectManager')
except:
    print 'Could not stop ArcServerObjectManager'
print '2 Execute cleanup of legacy files'
if arcpy.gp.exists(out_GDB,""):
    print '1A GDB already exists - proceed to 1AA to remove previous clip file.'
    try:
        arcpy.DeleteFeatures_management(out_GDB + out_Basic)
        print '1AA Successfully deleted previous temporary clip file: ' + out_GDB + out_Basic
    except:
        print '1AB Could not remove previous temporary clip file: ' + out_GDB + out_Basic
else:
    print '1B Creating Required Temp GDB in folder: ' + str(out_folder_path)
    arcpy.CreateFileGDB_management(out_folder_path, out_name)

if arcpy.gp.exists(out_GDB + out_Basic,""):
    arcpy.Delete_management(out_GDB + out_Basic,"")
    print '2A ' + out_GDB + out_Basic+ ' deleted.'
else:
    print '2B ' + out_GDB + out_Basic + ' doesn\'t exist.'

arcpy.geoprocessing.gp.setProduct("arcinfo")
arcpy.env.workspace = out_folder_path
datasetList = arcpy.ListWorkspaces("*.mdb", "All")
L = []
try:
    for dataset in datasetList:
        #print dataset
        L.append(dataset + out_Basic)
        print '  - ' + str(dataset) + '\\Basic added to the list.'
except:
    print "Error."

BasicMerged = out_GDB + out_Basic
arcpy.Merge_management(L,BasicMerged)

except:
    print "Now what do I do?"
   
os.system('sc \\\\myserver start ArcServerObjectManager')

now2 = datetime.datetime.now()
print 'Ending Script ' + str(now2.hour) + ":" + str(now2.minute) + ":" + str(now2.second)

Outcomes