Database Reference
In-Depth Information
Creating a geodatabase
You now know how to create a basic Python script; let's now learn how to use Python to
create our first file geodatabase programmatically. Esri has created a Python library called
arcpy where all the geodatabase operations can be called. We need to reference this lib-
rary using the import keyword in each script we write.
Note
The arcpy library is a Python library created by Esri that can be used to call ArcGIS geo-
processing tools from within Python.
Close any previously opened scripts and start a new Python script. We will start by import-
ing the arcpy library and declaring a few variables for the geodatabase name and path. To
start creating the file geodatabase, perform the following steps:
1. Write the following code into the Python editor. The sgdb_path variable is the
path in which we want to create the geodatabase, while sgdb_name is the name
of the geodatabase. Note that paths in Python are written with a common slash (/):
import arcpy
sgdb_path = "c:/gdb"
sgdb_name = "my_Python_gdb.gdb"
2. Executing the preceding code will not give you anything interesting just yet, as we
still have not called the function responsible for creating our geodatabase. The ar-
cpy.CreateFileGDB_management function accepts two parameters, the
path and the name of the geodatabase. So, go ahead and write this down and pass
in the two variables. Also, make sure to add a pause command at the end. Python is
case sensitive, so you have to write it exactly as you see it in the following code
snippet:
import arcpy
sgdb_path = "c:/gdb"
sgdb_name = "my_Python_gdb.gdb"
arcpy.CreateFileGDB_management(sgdb_path, sgdb_name)
input ("File created successfully, press any key to
continue...")
Search WWH ::




Custom Search