0

I am having trouble getting the os.path.exists to recognize that a file exists. I keep getting "File does not exist". I reduced the code to the absolute necessary to address my question. I believe everything relevant is in there.

import os
FileName15 ="Stuck"
Folded = arcpy.GetParameterAsText(4)
if Folded == '#' or not Folded:
    Folded = "C:\Documents \test.gdb\\" # provide a default value if unspecified

if os.path.exists(Folded+FileName15+"_table.dbf") == True:
    print ("It does exist")
    print Folded+FileName15+"_table.dbf"
    # Process: Delete
    arcpy.Delete_management(Folded+Name15+"_table", "Table")

    # Process Create Table
    arcpy.CreateTable_management(Folded, FileName15+"_table",Template_Shell, "")

elif os.path.exists(Folded+FileName15+"_table.dbf") == False:
    print ("File does not exist")
    print Folded+FileName15+"_table.dbf"
    # Process Create Table
    arcpy.CreateTable_management(Folded, FileName15+"_table",Template_Shell, "")
3
  • Please format your code correctly using the { } button. May 14, 2019 at 20:53
  • if you copy the full file name, open the python shell and paste it in the parameter for os.path.exists do you get True?
    – atxgis
    May 14, 2019 at 20:57
  • Have you made sure the code works before using arcpy.GetParameterAsText(4)?
    – Aaron
    May 15, 2019 at 2:43

2 Answers 2

11

A few things:

  1. Use "raw strings" to avoid issues with Windows path separators (backslashes) being interpreted as escape characters, e.g.

    Folded = r"C:\Documents \test.gdb"
             ^ note the r
    
  2. Is there really a space after "documents"? I suspect not

  3. Use os.path.join() to concatenate paths e.g.

    path = os.path.join(Folded, FileName15+"_table.dbf")

  4. There's no file on the filesystem which directly corresponds to a table/feature class in a gdb, only ArcGIS knows what's in these. What you really want is arcpy.Exists()

4
  • you are right on every count. I implemented your changes. Unfortunately now my script no longer recognizes arcpy as a global module. if arcpy.exists(table_name) == True: AttributeError: 'module' object has no attribute 'exists' May 14, 2019 at 21:24
  • 5
    Python is case sensitive. Use an upper case E in arcpy.Exists()
    – GBG
    May 14, 2019 at 21:47
  • 1
    @Damned_Newbie if the poster has answered your question, please press the little green tick next to his answer (top left). May 14, 2019 at 23:31
  • 1
    @Damned_Newbie you received an error because you used arcpy.exists which is not correct. It is arcpy.Exists() with capital E. Please look at the answer carefully. Here is the function from the esri help: desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-functions/…
    – ahmadhanb
    May 15, 2019 at 0:33
4

In addition to mikewatt's answer, you also seem to be checking of a table with extension .dbf exists inside the file geodatabase. ArcGIS tables stored in just a folder, outside of a FGDB, could have that extension, but when they are inside a FGDB, they do not have one.

2
  • Or you could look at the arcpy.Describe(your_data_element).dataType. DBF files return DbaseTable, Geodatabase tables return Table, feature classes in a geodatabase return FeatureClass and shapefiles return ShapeFile; unrecognized formats return either File or Folder for files and folders respectively. Trying to describe an object, file or folder that doesn't exist returns an IOError so it might be worth putting that into a try/except block or checking arcpy.Exists() first to ensure that something with the given identifier exists. May 15, 2019 at 4:34
  • There so many things wrong with my script but I finally got it does exist. Thank you all. May 15, 2019 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.