<?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: variable output file names in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/variable-output-file-names/m-p/426843#M33491</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I've never used ExtractByAttributes but after a quick scan of the help you could maybe try something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# Import system modules
import arcpy, os
from arcpy import env
from arcpy.sa import *

env.workspace = r"C:\rasters" # Whatever your workspace is
outPath =&amp;nbsp; r"C:\RasterExtracts" #Output Location for your raster attribute extract
i = 1111&amp;nbsp; # Moved this outside the loop so you don't get an infinite loop
while i &amp;lt; 111101:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sqlClause = "Value = " + str(i)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outRaster = ExtractByAttributes("Fac", sqlClause)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outRaster.save(outPath + os.sep + "Fac" + str(i))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i += 1111&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You have "fac" and "Fac" in your code.&amp;nbsp; Python will treat these differently.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;My understanding is that "fac" is just a string you want as part of the output file name.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Is "Fac" with the capital letter the name of your input raster?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In my code sample I just made the both caps because I am unsure what your input data is.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 19:13:06 GMT</pubDate>
    <dc:creator>JoelCalhoun</dc:creator>
    <dc:date>2021-12-11T19:13:06Z</dc:date>
    <item>
      <title>variable output file names</title>
      <link>https://community.esri.com/t5/python-questions/variable-output-file-names/m-p/426838#M33486</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am running a while loop and would like to name each output file using the iterator (i) as part of the file name. I have tried "fac" + i, "fac" + str(i), fac + str(i), and fac + i. None of these work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Does anyone know how this can be done?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;&amp;gt;&amp;gt;&amp;gt; while i &amp;lt; 111101: ...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i = 1111 ...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fac + i = ExtractByAttributes("Fac", "Value =" + i) ...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i = i + 1111 ...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Runtime error SyntaxError: can't assign to operator (&amp;lt;string&amp;gt;, line 3)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 May 2013 19:18:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/variable-output-file-names/m-p/426838#M33486</guid>
      <dc:creator>toddsams</dc:creator>
      <dc:date>2013-05-02T19:18:14Z</dc:date>
    </item>
    <item>
      <title>Re: variable output file names</title>
      <link>https://community.esri.com/t5/python-questions/variable-output-file-names/m-p/426839#M33487</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I am running a while loop and would like to name each output file using the iterator (i) as part of the file name. I have tried "fac" + i, "fac" + str(i), fac + str(i), and fac + i. None of these work.&lt;BR /&gt;&lt;BR /&gt;Does anyone know how this can be done?&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; while i &amp;lt; 111101:
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i = 1111
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fac + i = ExtractByAttributes("Fac", "Value =" + i)
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i = i + 1111
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
Runtime error SyntaxError: can't assign to operator (&amp;lt;string&amp;gt;, line 3)&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;edit: I see my solution wouldn't work....&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I don't quite understand why you are doing this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;

fac + i

&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Just convert the i value to a string:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
## updated
fac + &lt;STRONG&gt;str(i)&lt;/STRONG&gt; = ExtractByAttributes("Fac", "Value =" + &lt;STRONG&gt;str(i)&lt;/STRONG&gt;)

&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:12:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/variable-output-file-names/m-p/426839#M33487</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-12-11T19:12:56Z</dc:date>
    </item>
    <item>
      <title>Re: variable output file names</title>
      <link>https://community.esri.com/t5/python-questions/variable-output-file-names/m-p/426840#M33488</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I still get the error. I think the problem is with the fac + 1 at the beginning of line 3. I have tried using str(i) there too, and that does not help.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;gt;&amp;gt;&amp;gt; while i &amp;lt; 111101:
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i = 1111
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fac + i = ExtractByAttributes("Fac", "Value =" + str(i))
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i = i + 1111
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
Runtime error SyntaxError: can't assign to operator (&amp;lt;string&amp;gt;, line 3)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:12:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/variable-output-file-names/m-p/426840#M33488</guid>
      <dc:creator>toddsams</dc:creator>
      <dc:date>2021-12-11T19:12:58Z</dc:date>
    </item>
    <item>
      <title>Re: variable output file names</title>
      <link>https://community.esri.com/t5/python-questions/variable-output-file-names/m-p/426841#M33489</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I still get the error. I think the problem is with the fac + 1 at the beginning of line 3. I have tried using str(i) there too, and that does not help.&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;gt;&amp;gt;&amp;gt; while i &amp;lt; 111101:
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i = 1111
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fac + i = ExtractByAttributes("Fac", "Value =" + str(i))
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i = i + 1111
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
Runtime error SyntaxError: can't assign to operator (&amp;lt;string&amp;gt;, line 3)&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;post more of the code as it is hard to tell what fac is.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:13:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/variable-output-file-names/m-p/426841#M33489</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-12-11T19:13:01Z</dc:date>
    </item>
    <item>
      <title>Re: variable output file names</title>
      <link>https://community.esri.com/t5/python-questions/variable-output-file-names/m-p/426842#M33490</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;fac is not a variable. It is just a string I would like to use as the front end of my output file names. I'd like the file names to read fac1111, fac2222, fac3333 etc.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried putting fac in quotes as: "fac" + str(i) but this did not work either.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; while i &amp;lt; 111101:
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i = 1111
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "fac" + str(i) = ExtractByAttributes("Fac", "Value =" + str(i))
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i = i + 1111
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
Runtime error SyntaxError: can't assign to operator (&amp;lt;string&amp;gt;, line 3)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:13:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/variable-output-file-names/m-p/426842#M33490</guid>
      <dc:creator>toddsams</dc:creator>
      <dc:date>2021-12-11T19:13:04Z</dc:date>
    </item>
    <item>
      <title>Re: variable output file names</title>
      <link>https://community.esri.com/t5/python-questions/variable-output-file-names/m-p/426843#M33491</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I've never used ExtractByAttributes but after a quick scan of the help you could maybe try something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# Import system modules
import arcpy, os
from arcpy import env
from arcpy.sa import *

env.workspace = r"C:\rasters" # Whatever your workspace is
outPath =&amp;nbsp; r"C:\RasterExtracts" #Output Location for your raster attribute extract
i = 1111&amp;nbsp; # Moved this outside the loop so you don't get an infinite loop
while i &amp;lt; 111101:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sqlClause = "Value = " + str(i)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outRaster = ExtractByAttributes("Fac", sqlClause)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outRaster.save(outPath + os.sep + "Fac" + str(i))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i += 1111&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You have "fac" and "Fac" in your code.&amp;nbsp; Python will treat these differently.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;My understanding is that "fac" is just a string you want as part of the output file name.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Is "Fac" with the capital letter the name of your input raster?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In my code sample I just made the both caps because I am unsure what your input data is.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:13:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/variable-output-file-names/m-p/426843#M33491</guid>
      <dc:creator>JoelCalhoun</dc:creator>
      <dc:date>2021-12-11T19:13:06Z</dc:date>
    </item>
    <item>
      <title>Re: variable output file names</title>
      <link>https://community.esri.com/t5/python-questions/variable-output-file-names/m-p/426844#M33492</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The error message tells you the problem, you can't assign the result of an expression to another expression, only to a variable name.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Bad:&lt;/SPAN&gt;&lt;PRE class="plain" name="code"&gt;fac + i = ExtractByAttributes("Fac", "Value =" + i)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;SPAN&gt;Good:&lt;/SPAN&gt;&lt;PRE class="plain" name="code"&gt;#Use a list or a dict fac={} fac&lt;I&gt; = ExtractByAttributes("Fac", "Value =" + i)&amp;nbsp;&amp;nbsp; #Or use the eval function to evaluate a string expression (not as good...) eval('fac'+str(i)+'=ExtractByAttributes("Fac", "Value = ' + str(i) +'"')&amp;nbsp; #Or eval using string format codes eval('fac%s=ExtractByAttributes("Fac", "Value = %s"' % (i,i))&lt;/I&gt;&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Note the variable name is completely unrelated to the output file name... Are you saving the output to a file? If so, there's no reason to try and get the "i" value in the variable name, just use it in the output file name:&lt;/SPAN&gt;&lt;PRE class="plain" name="code"&gt;extracted = ExtractByAttributes("Fac", "Value = %s" % i) extracted.save("D:/data/output/fac"+str(i))&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 May 2013 20:13:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/variable-output-file-names/m-p/426844#M33492</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2013-05-02T20:13:14Z</dc:date>
    </item>
    <item>
      <title>Re: variable output file names</title>
      <link>https://community.esri.com/t5/python-questions/variable-output-file-names/m-p/426845#M33493</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks both Joel and Luke. This works, and now I have the concept of naming the file in the save function.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;-todd&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 May 2013 20:40:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/variable-output-file-names/m-p/426845#M33493</guid>
      <dc:creator>toddsams</dc:creator>
      <dc:date>2013-05-02T20:40:22Z</dc:date>
    </item>
  </channel>
</rss>

