flederfauz

Problem with arcpy.ListFields

Discussion created by flederfauz on Sep 13, 2011
Latest reply on Sep 21, 2011 by flederfauz
Hi!

I try to use the arcpy.ListFields command. It only works when I type in the name of the shape file directly. It does not work when I try to use a shapefile name from a list.
So to chekc my Code I added the print comand and it perfectly prints out the name of the shape.
Example: 'River' But the code still doesn't work. If I enter 'River' manually it works perfectly fine.

Any idea where the mistake might be?
Thanks a lot

Doesn't work:
while x <= leta:
        Listfieldx = str("'" + str(tableList[x])+"'")
        print Listfieldx
        fieldList = arcpy.ListFields(Listfieldx,"*","*")

I tried also fieldList = arcpy.ListFields(Listfieldx)
fieldList = arcpy.ListFields(str(Listfieldx)) etc, etc...

Does work:
while x <= leta:
        Listfieldx = str("'" + str(tableList[x])+"'")
        print Listfieldx
        fieldList = arcpy.ListFields('River')


Best regards
Chris
p.s. here the whole code
#Export all the field properties of each table in the Geodatabse
import arcpy
from arcpy import env

# Overwrite pre-existing files
arcpy.env.overwriteOutput = True
# Set the current workspace
#

# Set local variables
#Create a new table
try:
    geoDataBasePath = "P:/RAME/GIS/2011/Report.mdb"
    env.workspace = geoDataBasePath
    NewTable = "FieldProperties"
    arcpy.CreateTable_management(geoDataBasePath, NewTable)
        
    # For each field in the feature class, print all properties
    # Set local variables

    inFeatures = NewTable
    fieldName1="name"
    fieldName2="aliasName"
    fieldName3="baseName"
    fieldName4="domain"
    fieldName5="isNullable"
    fieldName6="precision"
    fieldName7="required"
    fieldName8="scale"
    fieldName9="type"
    fieldName10="length"
    fieldName11="editable"
    fieldName12="FC"
    fieldLength=255

    # Execute AddField new fields
    arcpy.AddField_management(inFeatures, fieldName1, "TEXT", "", "", fieldLength)
    arcpy.AddField_management(inFeatures, fieldName2, "TEXT", "", "", fieldLength)
    arcpy.AddField_management(inFeatures, fieldName3, "TEXT", "", "", fieldLength)
    arcpy.AddField_management(inFeatures, fieldName4, "TEXT", "", "", fieldLength)
    arcpy.AddField_management(inFeatures, fieldName5, "TEXT", "", "", fieldLength)
    arcpy.AddField_management(inFeatures, fieldName6, "TEXT", "", "", fieldLength)
    arcpy.AddField_management(inFeatures, fieldName7, "TEXT", "", "", fieldLength)
    arcpy.AddField_management(inFeatures, fieldName8, "TEXT", "", "", fieldLength)
    arcpy.AddField_management(inFeatures, fieldName9, "TEXT", "", "", fieldLength)
    arcpy.AddField_management(inFeatures, fieldName10, "TEXT", "", "", fieldLength)
    arcpy.AddField_management(inFeatures, fieldName11, "TEXT", "", "", fieldLength)
    arcpy.AddField_management(inFeatures, fieldName12, "TEXT", "", "", fieldLength)
    
    #List all tables in the Geodatabase
    tableList = arcpy.ListTables()
    for table in tableList:
        print table
        
    #how many tables are there
    leta = len(tableList)
    print leta
    x=0
    #loop through all tables
    while x <= leta:
        Listfieldx = str("'" + str(tableList[x])+"'")
        print Listfieldx
        fieldList = arcpy.ListFields(Listfieldx,"*","*")
        print str(fieldList)
        for field in fieldList:
            print 1
            NewList=[]
            fc=str(tableList[x])
            print fc
            NewList.extend ([str(fc),str(field.name),str(field.aliasName),str(field.baseName),str(field.domain),str(field.isNullable),str(field.precision),str(field.required),str(field.scale), str(field.type),str(field.length),str(field.editable)]) 
            print NewList
            cursor = arcpy.InsertCursor(fc)
            print 2
            row = cursor.newRow()
            row.FC=NewList[11]
            row.name = NewList[0]
            row.aliasName = NewList[1]
            row.baseName = NewList[2]
            row.domain = NewList[3]
            row.isNullable = NewList[4]
            row.precision = NewList[5]
            row.required = NewList[6]
            row.scale = NewList[7]
            row.type = NewList[8]
            row.length = NewList[9]
            row.editable = NewList[10]
            print 3
            cursor.insertRow(row)
            del NewList
            x=x+1
            del row
            print 99
    del cursor
    del NewList
except:
    print "\nError!\n"
    del row
    del cursor
    del NewList


 

Outcomes