<?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 Re: Add a definition Query with python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/add-a-definition-query-with-python/m-p/2050#M193</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you Owain,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;That did the trick. Did not&lt;BR /&gt;know about the save part. My fist try was a error because I had the mxd open in&lt;BR /&gt;arcmap. In other scripts a had lock errors. Same scripts work even if the mxd&lt;BR /&gt;is open. What are the rules for this ? where can I read about it ?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Greetings Peter&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 19 Feb 2015 13:50:07 GMT</pubDate>
    <dc:creator>PeterVersteeg</dc:creator>
    <dc:date>2015-02-19T13:50:07Z</dc:date>
    <item>
      <title>Add a definition Query with python</title>
      <link>https://community.esri.com/t5/python-questions/add-a-definition-query-with-python/m-p/2046#M189</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I want to add a difinition query with python so far i have this&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy&amp;nbsp; 
&amp;nbsp; 
mxd = arcpy.mapping.MapDocument(r"Y:\TRUTTA\Trutta_new\TruttaMaps\test.mxd")


for lyr in arcpy.mapping.ListLayers(mxd, "*"):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.name == "chimney_p":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.definitionQuery = """"Trutta_Map" = '19A4'"""&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In my pythonWin it seems to work BUT the query in my shape did not get the new query.&lt;/P&gt;&lt;P&gt;anyone know why that is ?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 20:05:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-a-definition-query-with-python/m-p/2046#M189</guid>
      <dc:creator>PeterVersteeg</dc:creator>
      <dc:date>2021-12-10T20:05:42Z</dc:date>
    </item>
    <item>
      <title>Re: Add a definition Query with python</title>
      <link>https://community.esri.com/t5/python-questions/add-a-definition-query-with-python/m-p/2047#M190</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The query may have work, but it isn't going to show up unless you are running the script against "CURRENT" See &lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#/MapDocument/00s30000000r000000/"&gt;example 2 in this link&amp;nbsp;&amp;nbsp; &lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 19 Feb 2015 08:19:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-a-definition-query-with-python/m-p/2047#M190</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2015-02-19T08:19:31Z</dc:date>
    </item>
    <item>
      <title>Re: Add a definition Query with python</title>
      <link>https://community.esri.com/t5/python-questions/add-a-definition-query-with-python/m-p/2048#M191</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Peter,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You are missing just the last step, saving the MXD when you have finished editing the layers.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So your code should read.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy

mxd = arcpy.mapping.MapDocument(r"Y:\TRUTTA\Trutta_new\TruttaMaps\test.mxd")

for lyr in arcpy.mapping.ListLayers(mxd, "*"):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.name == "chimney_p":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.definitionQuery = """"Trutta_Map" = '19A4'"""

mxd.save()&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope that helps,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Owain&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 20:05:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-a-definition-query-with-python/m-p/2048#M191</guid>
      <dc:creator>OwainCatton</dc:creator>
      <dc:date>2021-12-10T20:05:45Z</dc:date>
    </item>
    <item>
      <title>Re: Add a definition Query with python</title>
      <link>https://community.esri.com/t5/python-questions/add-a-definition-query-with-python/m-p/2049#M192</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;thank you Dan this gives my samething to read in the train and learn more about the mapping.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;gr Peter&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 19 Feb 2015 13:42:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-a-definition-query-with-python/m-p/2049#M192</guid>
      <dc:creator>PeterVersteeg</dc:creator>
      <dc:date>2015-02-19T13:42:43Z</dc:date>
    </item>
    <item>
      <title>Re: Add a definition Query with python</title>
      <link>https://community.esri.com/t5/python-questions/add-a-definition-query-with-python/m-p/2050#M193</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you Owain,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;That did the trick. Did not&lt;BR /&gt;know about the save part. My fist try was a error because I had the mxd open in&lt;BR /&gt;arcmap. In other scripts a had lock errors. Same scripts work even if the mxd&lt;BR /&gt;is open. What are the rules for this ? where can I read about it ?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Greetings Peter&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 19 Feb 2015 13:50:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-a-definition-query-with-python/m-p/2050#M193</guid>
      <dc:creator>PeterVersteeg</dc:creator>
      <dc:date>2015-02-19T13:50:07Z</dc:date>
    </item>
    <item>
      <title>Re: Add a definition Query with python</title>
      <link>https://community.esri.com/t5/python-questions/add-a-definition-query-with-python/m-p/2051#M194</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Afternoon Peter,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As Dan mentioned above, the use of &lt;STRONG&gt;CURRENT&lt;/STRONG&gt; instead of a file path would mean that the Python script will edit the current ArcMap session, where as if you specify the file path the Python script will open a new session, hence the locks errors your were getting as you had the MXD already open.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can read more here &lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//00s30000000r000000"&gt;http://resources.arcgis.com/en/help/main/10.2/index.html#//00s30000000r000000&lt;/A&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;STRONG&gt;MapDocument Example 1&lt;/STRONG&gt;&lt;BR /&gt;If you have a the MXD open in ArcMap and want to run a script on its contents use &lt;STRONG&gt;CURRENT&lt;/STRONG&gt;&lt;/LI&gt;&lt;LI&gt;&lt;STRONG&gt;MapDocument Example 2&lt;/STRONG&gt;&lt;BR /&gt;If you want to run the script independently you specifiy te full &lt;STRONG&gt;File Path&lt;/STRONG&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Owain&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 19 Feb 2015 13:59:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-a-definition-query-with-python/m-p/2051#M194</guid>
      <dc:creator>OwainCatton</dc:creator>
      <dc:date>2015-02-19T13:59:01Z</dc:date>
    </item>
  </channel>
</rss>

