Accessing shapefiles/dbfs within a geodatabase with python

6877
6
Jump to solution
12-30-2014 08:35 AM
ThomasCaruso
New Contributor III

Hi folks,

I feel like there's a very simple answer to this because no matter how much I searched I couldn't find one, however I'm pretty much brand new to gdb's, have mostly just used shapefiles up until now and I'm having trouble getting python to access the table of/a shapefile within a geodatabase. Here is the script I'm writing currently, can someone give me some direction here?

import arcpy
# Set workspace
arcpy.env.workspace = "C:\Users\tmc18\Desktop\comp_orps\NYS_Wells.gdb"

# Define input feature layer & wells
orps09 = "C:\Users\tmc18\Desktop\comp_orps\centroids\madirps_point_Layer.lyr"
wells = "C:\Users\tmc18\Desktop\comp_orps\NYS_Wells.gdb\Madison_DEC_Well_Logs_3_14_14_MASTER_COPY.dbf"
wellshp = "C:\Users\tmc18\Desktop\comp_orps\NYS_Wells.gdb\Madison_DEC_Well_Logs_3_14_14_MASTER_COPY.shp"

# Define fields cursors will require
county = "County"

# Create search cursor
with arcpy.da.SearchCursor(wells, [county]) as cursor:
        for row in cursor:
                if county == "MADISON":
                        arcpy.SelectLayerByLocation_management(orps09, [within_a_distance, "1 km", add_to_selection])
                        cursor.next(row)
                else:
                        continue

I'm getting errors because within the gdb, the dbf and shapefiles don't exist. Since I see them there in ArcCatalog I thought it might work anyways, but obviously that is not the case.

Many thanks in advance for your help,

Tom

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JoeFlannery
Occasional Contributor III

Do you need an r in front of your file paths?

Like this:

wells = r"C:\Users\tmc18\Desktop\comp_orps\NYS_Wells.gdb\Madison_DEC_Well.dbf" 

View solution in original post

0 Kudos
6 Replies
JoeFlannery
Occasional Contributor III

Do you need an r in front of your file paths?

Like this:

wells = r"C:\Users\tmc18\Desktop\comp_orps\NYS_Wells.gdb\Madison_DEC_Well.dbf" 

0 Kudos
ThomasCaruso
New Contributor III

This did it. Thank you.

0 Kudos
VinceAngelo
Esri Esteemed Contributor

Shapefiles are NOT stored within file geodatabases.  Each is a different format.  Data copied from a shapefile is manifested as a table in an FGDB.

You should not use a filesystem utility to place a shapefile in the ".gdb" directory of a file geodatabase. To copy the data into the FGDB, use ArcGIS or an application with the FGDB API access library enabled to create a new feature class table in the FGDB workspace. If you want to retain shapefile format, create a directory parallel to the .gdb (e.g., "NYS_Wells-shp"), and place the shapefiles there.

- V

JoeFlannery
Occasional Contributor III

Thomas:

For future readers of your posted question.

From the great book Python Scripting for ArcGIS, by Paul Zandbergen, Chapter 4.14 - Working with Paths:

There are three correct ways to specify a path in Python:

Use a forward slash

  “C:/EsriPress/Python/Data”

Use two backslashes

  “C:\\EsriPress\\Python\\Data”

Use a string literal by placing the letter r before the string

  r“C:\EsriPress\Python\Data”

curtvprice
MVP Esteemed Contributor

There's also a nice article in the ArcGIS Desktop help.

Paths explained: Absolute, relative, UNC, and URL

0 Kudos
XanderBakker
Esri Esteemed Contributor

Dear Thomas Caruso ‌,

Although Joe Flannery‌ may have answered your question and you are able to access your shapefile stored in the .gdb folder. this is not (and I repeat) *NOT* they way to solve your problem. As Vince Angelo‌ indicates, you should NOT store shapefiles in the .gdb folder. It may corrupt the other data in the file geodatabase, Please, please, *PLEASE*, remove the files from the .gdb folder and store them elsewhere.

For a list of file extensions you will want to move to another folder, see: Shapefile - Wikipedia, the free encyclopedia  or this old documentation: ArcGIS Desktop Help 9.2 - Shapefile file extensions