<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Best practice for paths - arcpy.env.workspace vs os.path.join in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/best-practice-for-paths-arcpy-env-workspace-vs-os/m-p/48812#M3872</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute. Parameters are not valid.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ERROR 000840: The value is not a Mosaic Dataset.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ERROR 000840: The value is not a Mosaic Layer.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ERROR 000840: The value is not a Raster Dataset.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Even though checking the input showed it to be valid raster dataset.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's the code that failed:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;arcpy.CalculateStatistics_management("demExtract","1","1","#","OVERWRITE")&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If I put the full path, it worked. Seems that setting arcpy.env.workspace didn't work for this tool. Using this worked:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;arcpy.CalculateStatistics_management((os.path.join(gdbPath, "demExtract")),"1","1","#","OVERWRITE")&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Is os.path.join something that should be used by default?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 21 May 2013 23:15:51 GMT</pubDate>
    <dc:creator>MatthewBrown1</dc:creator>
    <dc:date>2013-05-21T23:15:51Z</dc:date>
    <item>
      <title>Best practice for paths - arcpy.env.workspace vs os.path.join</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-for-paths-arcpy-env-workspace-vs-os/m-p/48812#M3872</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute. Parameters are not valid.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ERROR 000840: The value is not a Mosaic Dataset.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ERROR 000840: The value is not a Mosaic Layer.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ERROR 000840: The value is not a Raster Dataset.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Even though checking the input showed it to be valid raster dataset.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's the code that failed:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;arcpy.CalculateStatistics_management("demExtract","1","1","#","OVERWRITE")&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If I put the full path, it worked. Seems that setting arcpy.env.workspace didn't work for this tool. Using this worked:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;arcpy.CalculateStatistics_management((os.path.join(gdbPath, "demExtract")),"1","1","#","OVERWRITE")&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Is os.path.join something that should be used by default?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 May 2013 23:15:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-for-paths-arcpy-env-workspace-vs-os/m-p/48812#M3872</guid>
      <dc:creator>MatthewBrown1</dc:creator>
      <dc:date>2013-05-21T23:15:51Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice for paths - arcpy.env.workspace vs os.path.join</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-for-paths-arcpy-env-workspace-vs-os/m-p/48813#M3873</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Shouldn't:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.CalculateStatistics_management("demExtract"),"1","1","#","OVERWRITE")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;be&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.CalculateStatistics_management("demExtract","1","1","#","OVERWRITE")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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...&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 May 2013 23:50:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-for-paths-arcpy-env-workspace-vs-os/m-p/48813#M3873</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2013-05-21T23:50:36Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice for paths - arcpy.env.workspace vs os.path.join</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-for-paths-arcpy-env-workspace-vs-os/m-p/48814#M3874</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Shouldn't:&lt;BR /&gt;&lt;BR /&gt;arcpy.CalculateStatistics_management("demExtract"),"1","1","#","OVERWRITE")&lt;BR /&gt;be&lt;BR /&gt;arcpy.CalculateStatistics_management("demExtract","1","1","#","OVERWRITE")&lt;BR /&gt;&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Yep! Lazy mistake after copy &amp;amp; paste.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 May 2013 02:12:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-for-paths-arcpy-env-workspace-vs-os/m-p/48814#M3874</guid>
      <dc:creator>MatthewBrown1</dc:creator>
      <dc:date>2013-05-22T02:12:25Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice for paths - arcpy.env.workspace vs os.path.join</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-for-paths-arcpy-env-workspace-vs-os/m-p/48815#M3875</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Looks like Python code written by Esri uses underscores, for example look in:&lt;/P&gt;&lt;P&gt;C:\Program Files (x86)\ArcGIS\Desktop10.4\arcpy\arcpy\sa\Functions.py&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However many of the examples written by Esri in the arcpy reference use camelCase, but I've seen instances where they are inconsistent.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Python style guide says&amp;nbsp;lower_with_under&lt;/P&gt;&lt;P&gt;&lt;A class="link-titled" href="https://www.python.org/dev/peps/pep-0008/#method-names-and-instance-variables" title="https://www.python.org/dev/peps/pep-0008/#method-names-and-instance-variables"&gt;PEP 8 -- Style Guide for Python Code | Python.org&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Google style guide says&amp;nbsp;lower_with_under&lt;/P&gt;&lt;P&gt;&lt;A class="link-titled" href="https://google.github.io/styleguide/pyguide.html?showone=Naming#Naming" title="https://google.github.io/styleguide/pyguide.html?showone=Naming#Naming"&gt;Google Python Style Guide&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Oct 2016 02:10:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-for-paths-arcpy-env-workspace-vs-os/m-p/48815#M3875</guid>
      <dc:creator>RyanMonk1</dc:creator>
      <dc:date>2016-10-20T02:10:46Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice for paths - arcpy.env.workspace vs os.path.join</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-for-paths-arcpy-env-workspace-vs-os/m-p/48816#M3876</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The key is, ... it is a guide ... not rules .... can't find the exact link, but even Python's BDFL said as much&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Oct 2016 02:55:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-for-paths-arcpy-env-workspace-vs-os/m-p/48816#M3876</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2016-10-20T02:55:26Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice for paths - arcpy.env.workspace vs os.path.join</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-for-paths-arcpy-env-workspace-vs-os/m-p/48817#M3877</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 29 Oct 2016 02:39:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-for-paths-arcpy-env-workspace-vs-os/m-p/48817#M3877</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2016-10-29T02:39:50Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice for paths - arcpy.env.workspace vs os.path.join</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-for-paths-arcpy-env-workspace-vs-os/m-p/48818#M3878</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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 &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&amp;nbsp;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edit: Late to the party. Oh well.&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 10 Nov 2016 19:19:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-for-paths-arcpy-env-workspace-vs-os/m-p/48818#M3878</guid>
      <dc:creator>ClintonDow1</dc:creator>
      <dc:date>2016-11-10T19:19:17Z</dc:date>
    </item>
  </channel>
</rss>

