2

I have scripted a process to read a CSV file into a file geodatabase. Then, I want to read this imported file and work on it by adding new fields and calculating values. However, while using the arcpy.ListTables() on the geodatabase does not list the table.

I am just confused what is wrong.

Any help !!

Here is my script

outLocation = dbLoc+'\\'+ out_name +'.gdb'

# Make list of all tables in workspace
tables = arcpy.ListTables("GDB*")

print tables
try:

    for tb in tables:
    # Execute TableToGeodatabase
        print "Importing "+tb+ " to gdb: " + outLocation
        arcpy.TableToGeodatabase_conversion(tables, outLocation)

env.workspace = os.path.dirname(os.path.dirname(__file__))+'\\'+ out_name +'.gdb'
arcpy.RefreshCatalog(env.workspace)

inTables = arcpy.ListTables()
1

List tables uses the env.workspace setting, you are also asking for all tables that start with 'gdb'.

outLocation = dbLoc+"\\"+ out_name +".gdb"
arcpy.env.workspace = where_tables_are # the folder where your tables are
tables = arcpy.ListTables("GDB*.csv") # all tables starting with GDB, is this right
for tb in tables:
    # get the base name for the table
    # fred.csv becomes outTbName="fred", notUsed=".csv"
    outTbName,notUsed = os.path.splitext(tb)
    arcpy.AddMessage("Converting " + tb + " to " + outTbName)
    # clear the way for the new table
    if arcpy.Exists(outLocation + "\\" + outTbName):
        arcpy.AddMessage("Clearing " + outTbName)
        arcpy.Delete_management(outLocation + "\\" + outTbName)

    # copy the input rows
    arcpy.CopyRows_management(tb,outLocation + "\\" + outTbName)
# file is not defined, so set it to outLocation instead
# env.workspace = os.path.dirname(os.path.dirname(file))+"\\"+ out_name +".gdb"   
arcpy.env.workspace = outLocation
arcpy.RefreshCatalog(env.workspace)

inTables = arcpy.ListTables()

inTables should now be a list of all the tables in the geodatabase.

  • Michael, I tried your method and did specify the work space for my initial input files. So I was able to import all the files to the geodatabase. When i open the geodatabse via ArcGIS, I can see the imported tables. However when i try to read the tables from this geodatabse and list them the imported table are not listed. # all tables starting with GDB, is this right (yes this is correct) – Abhi May 27 '14 at 23:00
  • I'm not so sure about your workspace, os.path.dir... so set it instead to outLocation. – Michael Stimson May 27 '14 at 23:01
  • Ok. So I did check the workspace. As defined, it points to the correct GDB --> C:\Work\Projects\0b_CA_SFBayArea\Segmenter_v7\00001 CA San Francisco San Jose 2013.gdb. Also note that the listing statement lists two other tables already present in the geodatabase. Do you think there is something with importing csv. – Abhi May 27 '14 at 23:09
  • CSV can be a bit funny. To be safe I've used python file I/O to read and InsertCursor to put them into a database. Do you have Microsoft Access (or similar open office utility) by any chance? – Michael Stimson May 27 '14 at 23:16
  • I think its a CSV issue also, but cannot figure out what ? The script imports the table all OK. I can open it and see it. Yes, I have MS access, Postgres etc. – Abhi May 27 '14 at 23:19
1

You need to set the environment workspace to your input geodatabase before you call the ListTables line. Also I don't believe gdb* is a valid parameter, look at the help file to see the valid parameters.

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.