Best practice for paths - arcpy.env.workspace vs os.path.join

2349
6
05-21-2013 04:15 PM
MatthewBrown1
Occasional Contributor
Hi,

I've recently had an odd error and found it was due to the way I set up the input. The really odd thing is that some tools worked, but another (arcpy.CalculateStatistics_management) failed with the error:

Failed to execute. Parameters are not valid.
ERROR 000840: The value is not a Mosaic Dataset.
ERROR 000840: The value is not a Mosaic Layer.
ERROR 000840: The value is not a Raster Dataset.

Even though checking the input showed it to be valid raster dataset.

Here's the code that failed:

arcpy.CalculateStatistics_management("demExtract","1","1","#","OVERWRITE")


If I put the full path, it worked. Seems that setting arcpy.env.workspace didn't work for this tool. Using this worked:

arcpy.CalculateStatistics_management((os.path.join(gdbPath, "demExtract")),"1","1","#","OVERWRITE")


Is os.path.join something that should be used by default?
Tags (2)
0 Kudos
6 Replies
ChrisSnyder
Regular Contributor III
Shouldn't:

arcpy.CalculateStatistics_management("demExtract"),"1","1","#","OVERWRITE")
be
arcpy.CalculateStatistics_management("demExtract","1","1","#","OVERWRITE")

?

As long as your arcpy.env.workspace is set to where the raster "demExtract" lives, then it should work. I would refrain from naming files with camelCase and name it "dem_extract" instead.

That said about arcpy.env.workspace, I usually provide the tools with a datset's full path instead of relying on the assumption that all my input datasets are coming from the same workspace, which they often do not.

Per your question about a best practices, I am in the (bad) habit of using a harcoded '\\' as my path seperator, and it is much more correct to use os.path.join or os.path.sep to ensure that you are being OS-specific. I only use windows so '\\' works for me. However, if ArcGIS ever gets ported to UNIX or something, I'll be in a world of hurt updating my code...
0 Kudos
MatthewBrown1
Occasional Contributor
Shouldn't:

arcpy.CalculateStatistics_management("demExtract"),"1","1","#","OVERWRITE")
be
arcpy.CalculateStatistics_management("demExtract","1","1","#","OVERWRITE")



Yep! Lazy mistake after copy & paste.

Funny about the camelCase - I normally do use underscores but was told to change for this project. Does anyone have formal coding standards for Python and ArcGIS they can share?
0 Kudos
RyanMonk1
New Contributor III

Looks like Python code written by Esri uses underscores, for example look in:

C:\Program Files (x86)\ArcGIS\Desktop10.4\arcpy\arcpy\sa\Functions.py

However many of the examples written by Esri in the arcpy reference use camelCase, but I've seen instances where they are inconsistent.

Python style guide says lower_with_under

PEP 8 -- Style Guide for Python Code | Python.org 

Google style guide says lower_with_under

Google Python Style Guide 

DanPatterson_Retired
MVP Emeritus

The key is, ... it is a guide ... not rules .... can't find the exact link, but even Python's BDFL said as much

0 Kudos
ClintonDow1
Occasional Contributor II

Yes typically I would recommend getting used to PEP8 style, it is used for the majority of Python projects and is something that is appreciated during coding interviews  ArcPy actually breaks PEP8 in a lot of places for a number of historical reasons, so its not the best reference for the canonical Python style. 

Edit: Late to the party. Oh well. 

0 Kudos
curtvprice
MVP Esteemed Contributor

The biggest danger of using local path names I have run into is that they can conflict with layer names in your map. If you have a dataset named "foo" in your workspace and a layer named "foo" in your map (or in memory), the layer will be used. Took me a while to figure that one out.

0 Kudos