Select to view content in your preferred language

Problem with import table

2143
23
12-18-2012 10:58 AM
_ukaszZaremba
Emerging Contributor
Hello,

I have a source geodatabase with three tables: Table_A, Table_B and Table_C.
I would like to export two of them to another geodatabase.
The list of tables I would like to export is written in txt files.

I've written a script in Python to do that but I have an error when I run it.


When the list contains only one table's name, the export is accomplished correctly.

Can You help mi with this script?

import arcpy

source="C:\\Data\\Data.gdb\\"
destination="C:\\Data\\Base.gdb"
Tables="C:\\Data\\Tables.txt"

w=open(Tables,'r')
lines=w.readlines()
for line in lines:
arcpy.TableToTable_conversion(source+line,destination,line)
w.close()


Error:
"arcgisscripting.ExecuteError: ERROR 999999: Error executing function.
The table was not found.
The table was not found. [Table_A]
Failed to execute (TableToTable)."
Tags (2)
0 Kudos
23 Replies
T__WayneWhitley
Honored Contributor
If I can jump in here, I think you'd better check your pathname strings - I always use something like:

source= r'C:\\Data\Data.gdb'



Also, if in doubt print it out.... promise you I didn't mean for the word play...but check it with a print statement.  Reversing the '\\' to '//' I think is standard, but when my eyes are fatigued at the end of the day, that's is certain to irritate me to discover that I forgot one of those.  Use 'r' not as the only failsafe, but check it too with a simple print, if you have to troubleshoot.

And I agree with Caleb below -- why do this and risk file-locking or a mistake in the path, etc., not when you can read it directly.  But I cannot assume to know why you are recording this to txt.
0 Kudos
by Anonymous User
Not applicable
In theory, what you were trying to do with the text file should work.  However, I do not know what your text file looks like so I can't answer as to what is wrong there.  However, that is probably unnecessary and you are only creating extra work for yourself.

Using the ListTables()  or Wayne's suggestion with the list "for table in ['Table_A','Table_B']:" would be much better.  Also, one more tip is it is recommended to use the "os.path.join" (I have shortened it to p.join) to append path names rather than using the + and '\\' routine.  If you could post what your text file looks like I can try to help you find the problem.
0 Kudos
T__WayneWhitley
Honored Contributor
Just an additional note due to my curiosity --
It could be confusing that the syntax of TableToTable has the 1st 3 params as TableName, destination workspace, TableName.

So, KPG, you could do this with the variables you established provided they are valid:

arcpy.env.workspace = source
arcpy.TableToTable_conversion(line, destination, line)

In fact you should probably make sure everything really exists before you get to the conversion line with something like:
if arcpy.Exists(line):
     print 'all is well'
     # proceed with processing
else:
     print 'uh-oh, better check this:  ' + line
     # could even raise an exception here, if you wanted
0 Kudos
_ukaszZaremba
Emerging Contributor
Thank you for hints. I change my script:

import arcpy, os
from os import path as p

source= r'C:\Data\Data.gdb'
destination= r'C:\Data\Base.gdb'
Tables= r'C:\Data\Tables.txt'

w=open(Tables,'r')
lines=w.readlines()
for line in lines:
print (p.join(source,line))
arcpy.TableToTable_conversion(p.join(source,line),destination,line)
w.close()


and I still have the same error.


I attach jpg file to show the paths to my data and my txt file. I think that paths are correct.
Your solution are better than use txt file but I am new in Python and I try to understand why it does not work :).
I apologize for this madness 🙂

[ATTACH=CONFIG]20067[/ATTACH]
0 Kudos
T__WayneWhitley
Honored Contributor
Do you use IDLE?  Your script is so short you could quickly execute line by line and isolate where the error is occurring, although I suspect there's a problem with what is being returned on reading the txt, and consequently the conversion fails.
Use the arcpy.Exists statement after you join the pathname to see if it 'makes sense' to arcpy.

Also, when you paste code, use the tags to preserve intention (use the # symbol or manually enter after the code and
 before the code to set it apart).  Maybe I missed it but did you attach any samples?
0 Kudos
T__WayneWhitley
Honored Contributor
"...preserve indention...", I meant to say - all text pasted here otherwise loses formatting.
0 Kudos
by Anonymous User
Not applicable
I was able to test to see if paths exist while table names are being read from a text file with the following code:

import arcpy
from os import path as p

arcpy.env.workspace = source = r'G:\PROJECTS\Cedar\Address\Tables'
tables = open(r'C:\Documents and Settings\gis\Desktop\Tables.txt', 'r')
for table in tables.readlines():
    full = p.join(source, table)
    print full
    if arcpy.Exists(full):
        print 'True'
    else:
        print 'False'


I would still recommend using the ListTables() function though.  Here is a look at my results and text file:

[ATTACH=CONFIG]20068[/ATTACH]
0 Kudos
T__WayneWhitley
Honored Contributor
Take a breath - I believe this may anger you, but this type of silly 'madness' happens more frequently than you may think...yes, silly error, but here I think is the source of your problem:

When I print your 'lines' variable (see below), I get the 'extra formatting' included - pretty certain you have to 'handle this' in your code because certainly arcpy doesn't know what the 'readlines' method is doing.  Here's the trouble:

>>> print lines
['Table_A\n',  'Table_B']

What clued me in is that you said in your 1st message that you had no trouble when the file is populated with one table, so the crlf (carriage return linefeed) character is then included when you enter successive lines in the txt.  Sorry for not seeing that earlier.

EDIT:
Right, so a simple solution is a 'slicing' trick [0:-1], to include with fetching the 'line':
(note: Make sure that for the last line in your file, you enter a line return before saving.  Sometimes it's consider a better practice to include an END or endfile statement.)
for line in lines:
     line = line[0:-1]
     print line
     # feed this into the path join, etc.
0 Kudos
T__WayneWhitley
Honored Contributor
Sorry about this, but now I have solid proof - and this is an important subtle concept, so bear with me:

Your error message stemmed from the line continuation character, this is true; however, it is important to investigate your error messages carefully, as the ERROR 999999 is a generic code.  Exercise care with your variable references too, see (along with the attachment) the following interactive IDLE session (I apologize for the readibility; if you are having problems, it may be easier to read if you copy/paste into a text editor - I didn't intend for the text-wrapping as shown below):
>>> # Tables is my ref to the txt...
>>> w = open(Tables, 'r')
>>> for table in w.readlines():
              # if I apply the text 'slicing' in the path join part of the process,
              # no change is applied where it is also needed for the 'table' var param in TableToTable:
 full = os.path.join(source, table[0:-1])
 if arcpy.Exists(full):
  print 'True'
  arcpy.TableToTable_conversion(full, r'C:\Data\base.gdb', table)
  print 'success converting: ' + table
 else:
  print 'False'

  
True

Traceback (most recent call last):
  File "<pyshell#48>", line 5, in <module>
    arcpy.TableToTable_conversion(full, r'C:\Data\base.gdb', table)
  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\conversion.py", line 1463, in TableToTable
    raise e
ExecuteError: ERROR 999999: Error executing function.
Failed to execute (TableToTable).

>>> # So what just happened??
>>> # This time the table was found, but the conversion could not be completed...same generic error code.
>>> # But if you refresh Catalog, you'll see a strange entry, Table_A with a 'special character' appended.
>>> # This is how the character is interpreted within ArcGIS...notice that the source table was found above...'True' returned.
>>> # BUT, processing halted.

>>> # Try again, this time intervening to redefine the 'table' variable for both the input table source and the output name:
>>> # (The conversion is completed.  See the attached illustration that includes the Table_A incomplete table execution.)
>>> w.close()
>>> w = open(Tables, 'r')
>>> for table in w.readlines():
 table = table[0:-1]
 print "table: " + "'" + table + "'"
 full = os.path.join(source, table)
 print 'full pathname: ' + "'" + full + "'"
 if arcpy.Exists(full):
  print 'True - table exists: ' + full
  arcpy.TableToTable_conversion(full, r'C:\Data\base.gdb', table)
  print 'success converting: ' + table
 else:
  print 'False'

  
table: 'Table_A'
full pathname: 'C:\Data\Data.gdb\Table_A'
True - table exists: C:\Data\Data.gdb\Table_A
<Result 'C:\\Data\\base.gdb\\Table_A'>
success converting: Table_A
table: 'Table_B'
full pathname: 'C:\Data\Data.gdb\Table_B'
True - table exists: C:\Data\Data.gdb\Table_B
<Result 'C:\\Data\\base.gdb\\Table_B'>
success converting: Table_B
>>> 
0 Kudos
by Anonymous User
Not applicable
Strange, why do I not receive this same error?  My text file does not appear to have the continuation character.  My code is pretty much the same as KPG's:

import arcpy
from os import path as p

arcpy.env.workspace = source = r'G:\PROJECTS\Cedar\Address\Tables'
gdb = p.join(source,'Test_db.gdb')
tables = open(r'C:\Documents and Settings\gis\Desktop\Tables.txt', 'r')
for table in tables.readlines():
    full = p.join(source, table)
    print full
    if arcpy.Exists(full):
        print 'True'
        arcpy.TableToTable_conversion(full, gdb, table.split('.')[0]) # removes '.dbf'
        print 'Converted %s to %s' %(table,gdb)
    else:
        print 'False'
0 Kudos