Select to view content in your preferred language

Python Scripting for Geoprocessing Workflows (for ArcGIS 10) - Code Will Not Work

889
4
02-01-2012 03:06 PM
BrittanyGale
Frequent Contributor
Hello! I am taking the virtual Esri class Python Scripting for Geoprocessing Workflows (for ArcGIS 10) and am having trouble executing some of the sample code.

This is the exact code from the course: fcList = arcpy.ListFeatureClasses("C:/Data/SanJuan.gdb")
This is the code that I am using: testList = arcpy.ListFeatureClasses("H:\Gale_Intermediate_GIS_Data\Gale_Scratch.gdb")

When I execute "print testList", a blank list displays: []
The pathname I am using definitely exists. There are no spaces or special characters. There are 9 feature classes within the geodatabase. Esri Tech Support said that they can only help me if my problem exists within their exercises, not their sample code, which makes little sense to me.

What am I doing wrong?!
Tags (2)
0 Kudos
4 Replies
HenryColgate
Regular Contributor
I think it is because you are using back slashes not forward slashes.

Back slash is an exit code in Python that is used to insert special characters and other info.


print "Hello\nWorld"



Would result in output:

Hello
World

rather than:

Hello\nWorld

as you may have expected.

This is because in Python "\n" refers to a new line rather than the backslash character and the letter n.  Other examples are "\r" for carriage return and "\t" for tab. 

Either change the back slash to forward slash "/" as shown in the code or Double up the back slash e.g. "\\" so you are confirming that backslash is what you wish to use.
0 Kudos
AndrewChapkowski
Esri Regular Contributor
Try putting a r infront of your path:

Code Example:
>>> path = r"c:\temp\scratch.gdb"
>>> print path
c:\temp\scratch.gdb
>>> path = "c:\temp\scratch.gdb"
>>> print path
c: emp\scratch.gdb
0 Kudos
BrittanyGale
Frequent Contributor
Thank you for you help! I hadn't realized that my pathname included single backslashes. I learned how to enter pathnames (r"...",  \\, or /), but I just didn't catch my syntax error. I felt so silly when I read your responses. But, unfortunately, when I fixed this syntax error, it did not fix the problem. I called Esri Support again, but was lead to a different type of analyst. She helped me fix the problem, but the resulting solution leads us to beleive that the online course I am taking provides bad or incomplete information. Here is an explanation:

Esri says that I can create a list of feature classes by specifying the geodatabase pathname using the following code:
fcList = arcpy.ListFeatureClasses("C:/Data/SanJuan.gdb")

What they did not mention was that a workspace needs to be set first, and that the list is created using the workspace.
Source: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v0000001n000000

The code that ended up working was this:
>>> import arcpy
>>> arcpy.env.workspace = "Q:\Applications\GIS\BGale\AddToSDE.gdb"
>>> fcList = arcpy.ListFeatureClasses()
>>> print fcList
[u'Grid_DetailedMaps', u'Grid_Original', u'RightOfWays_StreetDisplay', u'RoadMedians', u'CensusZipCodes_Edited_ClippedToCityLimit']


So, I am happy that I fixed the problem, but I am unhappy that the online course did not explain this.

Have a wonderful day everyone!
0 Kudos
BrittanyGale
Frequent Contributor
One more thing...

Esri technical support said that their sample code is not intended to be executed. What the hell. That is rediculous in my opinion, but they do explain the full code in the exercise.
0 Kudos