Select to view content in your preferred language

arcpy.Exists fails

4171
8
Jump to solution
01-29-2016 07:44 AM
KennethKirkeby
Deactivated User

ArcGIS 10.3.1; Python 2.7.11

The file DOES exist but reporting that it does not. The syntax looks correct to my newbie eyes.

******************************************************************************************************************

import arcpy

arcpy.env.workspace = "C:\\Users\\kkirkeby\\Desktop\\GIS-4080\\Lesson4_Data\\Lesson4_Data"

#Variables

out_folder_path = 'C:\\Users\\kkirkeby\\Desktop\\GIS-4080\\Lesson4_Data\\Lesson4_Data'

gdb_name = 'L4E5'

gdb_fullPath = out_folder_path +'/' + gdb_name

fclist = arcpy.ListFeatureClasses()

fcIn = 0

fcOut = 0

# Create gdb if it doesn't exist.

if arcpy.Exists(gdb_fullPath):

    print "Geodatabase already exist"

else:

       print "Geodatabase does not exist. Creating..."

       print arcpy.Exists(gdb_fullPath)

       print gdb_fullPath

#    arcpy.CreateFileGDB_management(out_folder_path, gdb_name)

******************************************************************************************************************

Output:

================================ RESTART ================================

Geodatabase does not exist. Creating...

False

C:\Users\kkirkeby\Desktop\GIS-4080\Lesson4_Data\Lesson4_Data/L4E5

0 Kudos
1 Solution

Accepted Solutions
JamesCrandall
MVP Frequent Contributor

This:

C:\Users\kkirkeby\Desktop\GIS-4080\Lesson4_Data\Lesson4_Data\l4_ex5

Is not a geodatabase/workspace.  You are missing the ".gdb" at the end.

C:\Users\kkirkeby\Desktop\GIS-4080\Lesson4_Data\Lesson4_Data\l4_ex5.gdb

View solution in original post

8 Replies
DanPatterson_Retired
MVP Emeritus

can you try concatenating your file with "\\" instead of "/" the inconsistency in the final version may be confusing

not specific to gdb's but

Filenames and file paths in Python

example

>>> f = "file"
>>> path = r"c:\temp\text.gdb"
>>> full = path+ "\" + f
  File "<string>", line 1
    full = path+ "\" + f
                       ^
SyntaxError: EOL while scanning string literal
>>> full = path+ "\\" + f
>>> full
'c:\\temp\\text.gdb\\file'

>>> full
'c:\\temp\\text.gdb/file'
# works but may be confusing
KennethKirkeby
Deactivated User

Same result.

0 Kudos
JamesCrandall
MVP Frequent Contributor

You can try:

gdb_name = 'L4E5.gdb'
gdb_fullPath = out_folder_path +'\' + gdb_name

Although I typically would construct strings a bit differently using the 'r' literal:

out_folder_path = r'C:\Users\kkirkeby\Desktop\GIS-4080\Lesson4_Data\Lesson4_Data'
gdb_name = 'L4E5.gdb'
gdb_fullPath = out_folder_path +'\' + gdb_name
0 Kudos
KennethKirkeby
Deactivated User

Same result:

Also change

gdb_fullPath = out_folder_path +'\\' + gdb_name

print out_folder_path = C:\Users\kkirkeby\Desktop\GIS-4080\Lesson4_Data\Lesson4_Data

print gdb_name = l4_ex5

print gdb_fullPath = C:\Users\kkirkeby\Desktop\GIS-4080\Lesson4_Data\Lesson4_Data\l4_ex5

print arcpy.Exists(gdb_fullPath) = False

0 Kudos
JamesCrandall
MVP Frequent Contributor

This:

C:\Users\kkirkeby\Desktop\GIS-4080\Lesson4_Data\Lesson4_Data\l4_ex5

Is not a geodatabase/workspace.  You are missing the ".gdb" at the end.

C:\Users\kkirkeby\Desktop\GIS-4080\Lesson4_Data\Lesson4_Data\l4_ex5.gdb

KennethKirkeby
Deactivated User

Well there's 4 hours I'll never get back ...

Thanks. Details, details, details.

DanPatterson_Retired
MVP Emeritus

You will regain it in the future since you will not likely make that mistake again... Now if we could get people to quit using paths with spaces in them...

KennethKirkeby
Deactivated User

I had read the link you listed before and thought I was good. I like your comment about using os.path instead.

Thanks for all your help to date.

Ken