flederfauz

Code working in Python Python Window but not as Script

Discussion created by flederfauz on Sep 20, 2011
Latest reply on Sep 18, 2017 by ordoinc
Hi!
So I wrote some Python code to List all Maps in a folder and some more properties and stuff. Everything keeps running perfectly smooth if I use it in the ArcmapGeoprocessing/Python Command Window.
But if I want to run it as a Script it fails. Giving me this Error message
<class 'arcgisscripting.ExecuteError'>: Failed to execute. Parameters are not valid.
ERROR 000732: Input Table: Dataset MXD_Overview_Table does not exist or is not supported
Failed to execute (AddField).


The new table (MXD_Overview_Table) is created, but for some reason Python can not add fields if running as a script. What is it I am missing?
Thanks for your help
Chris
P.S: I ve Windows 7 for 64 bit, ARCGIS10 Editor Licence with Service Pack 2 installed, on a Dell with a Intel i 7 and 8GB of Ram and plenty of space on my harddisk.

My Code is:
#List all maps and there properties in a folder 
# Import system modules
import arcpy, os, glob
import sys
import traceback
# Overwrite pre-existing files
arcpy.env.overwriteOutput = True
#This is the folder containing your MXD's
arcpy.env.workspace = r"P:/RAME/GIS/2011/Maps" #arcpy.GetParameterAsText(0)
baseFolder=arcpy.env.workspace
#The Geodatabase to store the Resulting table in
geoDataBasePath="P:/RAME/GIS/2011/RAME.gdb" #arcpy.GetParameterAsText(1)
#The Name of the New Table containing the MXD-List
NewTable="MXD_Overview_Table" #arcpy.GetParameterAsText(2)
#Define Workspace
#Create New Lists for Layers and Sources of Layers
mxdFileNames=[]
workspaces=[baseFolder]
#Create a new table containing Name of MXD, Path of MXD, Selected MXD Properties, Layers in the MXD and Sources (Shapefiles on which the Layers are based of the MXD) 
field_length=255
field_length2=3000
arcpy.CreateTable_management(geoDataBasePath, NewTable)
arcpy.AddField_management(NewTable, "MXD_Name", 'TEXT', '', '', field_length, "MXD_Name")
arcpy.AddField_management(NewTable, "MXD_Path", 'TEXT', '', '', field_length, "MXD Fullpath")
arcpy.AddField_management(NewTable, "Author", 'TEXT', '', '', field_length, "Authors of MXD")
arcpy.AddField_management(NewTable, "Credits", 'TEXT', '', '', field_length, "Credits")
arcpy.AddField_management(NewTable, "Descript", 'TEXT', '', '', field_length2, "Map Description")
arcpy.AddField_management(NewTable, "Summary", 'TEXT', '', '', field_length2, "Map Summary")
arcpy.AddField_management(NewTable, "Tags", 'TEXT', '', '', field_length, "Map Tags")
arcpy.AddField_management(NewTable, "Saved", 'TEXT','','',field_length2,"Date Saved")
arcpy.AddField_management(NewTable, "Layers", 'TEXT', '', '', field_length2,"Layers included in Map")
arcpy.AddField_management(NewTable, "FeatureCl", 'TEXT', '', '', field_length2, "Featureclasses included in Map")
#Lets grab a listing of all MXD's in our workspace; baseFolder
b = arcpy.ListWorkspaces("*", "Folder")
workspaces.extend(b)
for workspace in workspaces:
    #Lets grab a listing of all MXD's in our workspace; baseFolder
    allItems = os.listdir(workspace)
    # filter out just .mxd's
    mxdFileNames = [(x) for x in allItems if x.endswith('.mxd')]
    #For each mxd get all parameters. To do this step through the mxd list
    for name in mxdFileNames:
        try:#get mxd
            arcpy.env.workspace=baseFolder
            temp= str(str("%s" %workspace)+"\\"+name)
            mxd = arcpy.mapping.MapDocument(temp)
            namemxd= str("%s" % mxd.title)
            filePath=str(mxd.filePath)
            author="%s" % mxd.author
            credits_="%s" % mxd.credits
            dateSaved="%s" % mxd.dateSaved
            description="%s" % mxd.description
            summary="%s" % mxd.summary
            tags="%s" % mxd.tags
            #list all the layers for MXD after another from above
            try:
                    Source=[]
                    LyrList=[]
                    mxdlyrs=arcpy.mapping.ListLayers(mxd)
                    for lyr in mxdlyrs:
                        name=lyr.longName
                        if lyr.supports("dataSource"):
                            dataSource=lyr.dataSource
                        else:
                            dataSource="Not defined"
                        LyrList.extend([str(name)])
                        Source.extend([str(dataSource)])
            except ValueError:
                    #get error message if there is a problem getting lyr name or lyr source
                    print "Invalid Layer in MXD:",filePath,"."
                    arcpy.AddError( 'Invalid Layer in MXD:',filePath, '. Maybe because of a missing template.')
            finally:
                #Create a Insert Cursor to insert new rows and write each value in the corresponding field
                cursor = arcpy.InsertCursor(geoDataBasePath+"\\"+NewTable)
                row = cursor.newRow()
                row.MXD_Name=str(namemxd)
                row.MXD_Path=str(filePath)
                if len(LyrList)>3000:
                    LyrList=["More than 3000 characters. To many to be written to the file"]
                else:
                    row.Layers=str(LyrList)
                if len(Source)>3000:
                    Source=["More than 3000 characters. To many to be written to the file"]
                else:
                    row.FeatureCl=str(Source)
                row.Author=author
                row.Credits=str(credits_)
                row.Descript=str(description)
                row.Saved=str(dateSaved)
                row.Summary=str(summary)
                row.Tags=str(tags)       
                cursor.insertRow(row)
                 # tidy up
                del row
                del cursor
        except ValueError:
            print "Invalid MXD:",filePath,". Open the mxd in ArcMap and check for errors."
            arcpy.AddError( 'Invalid MXD:',filePath, '. Open the mxd in ArcMap and check for errors.')    
       

Outcomes