<?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: Name output files in &amp;quot;for&amp;quot; loop using input filenames in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/name-output-files-in-amp-amp-quot-for-amp-amp-quot/m-p/444934#M34851</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Bruce, thank you--that is exactly what I was looking for. Stacy, indexing incrementally shouldn't be necessary here, as the slicing operation will preserve the Unique ID in the filename, but I'll stash that idea for the future. I'll let you know how it goes!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 26 Mar 2012 13:49:34 GMT</pubDate>
    <dc:creator>AnneRiddle</dc:creator>
    <dc:date>2012-03-26T13:49:34Z</dc:date>
    <item>
      <title>Name output files in &amp;amp;quot;for&amp;amp;quot; loop using input filenames</title>
      <link>https://community.esri.com/t5/python-questions/name-output-files-in-amp-amp-quot-for-amp-amp-quot/m-p/444931#M34848</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi all, &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Apologies if this has been answered elsewhere; I'm a relative newcomer to Python (know just enough to get myself in trouble, usually). I'm working on a large script to geoprocess hundreds of files which will require outputs for each tool to be named as the for loop goes over them. Here's an example of part of the code: &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;for fc in arcpy.ListFeatureClasses ("*", "Point"):&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; outmean= **** &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MeanCenter_stats ('fc', 'outmean')&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Where the ***** definition must iteratively rename the output file based on the input filename. The input files contain unique identifiers in the filename, like so:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;samp01_0020_1986to1992_change_60m&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;samp01_0074_1986to1992_change_60m&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;samp05_0031_1992to2000_change_60m&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The output file needs to contain the first 22 characters (i.e. samp01_0020_1986to1992) in order to be identified in the final output. Therefore, CreateUniqueName and CreateScratchName seem inappropriate because this part of the path couldn't be preserved (unless I'm mistaken). I thought some operation using os.path.join would be most appropriate, but the syntax is unclear to me--do I need to create a list of filenames, or can I use something like this: &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;for raster in arcpy.ListRasters:&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; outpoint = os.path.join (raster, "out") &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.ExtractbyAttributes (raster, " VALUE = ", outpoint)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In addition, if I wanted to strip the end of the file (say, take off "change_60m") and add another ending, where should I start with the syntax? os.path.split and os.path.splitext seem inappropriate here, as I'm not interested in the entire path or the extension (neither are relevant, as the workspace is a single geodatabase).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks in advance.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Mar 2012 18:44:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/name-output-files-in-amp-amp-quot-for-amp-amp-quot/m-p/444931#M34848</guid>
      <dc:creator>AnneRiddle</dc:creator>
      <dc:date>2012-03-23T18:44:32Z</dc:date>
    </item>
    <item>
      <title>Re: Name output files in "for" loop using input filenames</title>
      <link>https://community.esri.com/t5/python-questions/name-output-files-in-amp-amp-quot-for-amp-amp-quot/m-p/444932#M34849</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You want to use slicing to extract a subset of a string. To get the first 22 characters of a string use:&lt;/SPAN&gt;&lt;PRE class="plain" name="code"&gt;outmean = fc[:22]&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;SPAN&gt;which returns the characters in positional offset 0 thru 21. Then append the rest of your output file name with the append symbol, '+'. To complete it all in one step, it will look something like:&lt;/SPAN&gt;&lt;PRE class="plain" name="code"&gt;outmean = fc[:22] + "newending"&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Mar 2012 19:12:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/name-output-files-in-amp-amp-quot-for-amp-amp-quot/m-p/444932#M34849</guid>
      <dc:creator>BruceNielsen</dc:creator>
      <dc:date>2012-03-23T19:12:45Z</dc:date>
    </item>
    <item>
      <title>Re: Name output files in "for" loop using input filenames</title>
      <link>https://community.esri.com/t5/python-questions/name-output-files-in-amp-amp-quot-for-amp-amp-quot/m-p/444933#M34850</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Anne,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Bruce's answer should work. I just noticed that you need to be careful with your variables, i.e. where these are already strings. For example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for fc in arcpy.ListFeatureClasses ("*", "Point"): 
&amp;nbsp;&amp;nbsp;&amp;nbsp; outmean= ****
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MeanCenter_stats ('fc', 'outmean')&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;You have &lt;/SPAN&gt;&lt;STRONG&gt;'fc'&lt;/STRONG&gt;&lt;SPAN&gt;, but &lt;/SPAN&gt;&lt;STRONG&gt;fc&lt;/STRONG&gt;&lt;SPAN&gt; is already a string - it is a variable, whose name is fc, that contains the feature class name, i.e. '2009_Street_Centrelines'... If you wrap the fc in quotes, then you are passing a new string, literally just &lt;/SPAN&gt;&lt;STRONG&gt;fc&lt;/STRONG&gt;&lt;SPAN&gt;, not the contents of the variable &lt;/SPAN&gt;&lt;STRONG&gt;fc&lt;/STRONG&gt;&lt;SPAN&gt;...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Not sure if you require this, but if you need things to be unique, you can just make a custom counter to ensure uniqueness of the names, i.e.:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
index = 1
for fc in arcpy.ListFeatureClasses ("*", "Point"): 
&amp;nbsp;&amp;nbsp;&amp;nbsp; outmean= fc[:22] + '_' + index
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MeanCenter_stats (fc, outmean) # ok, I don't know what exactly is going on here, but hopefully that works...
&amp;nbsp;&amp;nbsp;&amp;nbsp; index += 1 # increment the index&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Let me know how you get on!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 16:29:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/name-output-files-in-amp-amp-quot-for-amp-amp-quot/m-p/444933#M34850</guid>
      <dc:creator>StacyRendall1</dc:creator>
      <dc:date>2021-12-12T16:29:32Z</dc:date>
    </item>
    <item>
      <title>Re: Name output files in "for" loop using input filenames</title>
      <link>https://community.esri.com/t5/python-questions/name-output-files-in-amp-amp-quot-for-amp-amp-quot/m-p/444934#M34851</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Bruce, thank you--that is exactly what I was looking for. Stacy, indexing incrementally shouldn't be necessary here, as the slicing operation will preserve the Unique ID in the filename, but I'll stash that idea for the future. I'll let you know how it goes!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Mar 2012 13:49:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/name-output-files-in-amp-amp-quot-for-amp-amp-quot/m-p/444934#M34851</guid>
      <dc:creator>AnneRiddle</dc:creator>
      <dc:date>2012-03-26T13:49:34Z</dc:date>
    </item>
    <item>
      <title>Re: Name output files in "for" loop using input filenames</title>
      <link>https://community.esri.com/t5/python-questions/name-output-files-in-amp-amp-quot-for-amp-amp-quot/m-p/444935#M34852</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You're Welcome. If you click the checkmark next to the correct response, it will let others doing searches later on know that your question was answered.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Mar 2012 14:01:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/name-output-files-in-amp-amp-quot-for-amp-amp-quot/m-p/444935#M34852</guid>
      <dc:creator>BruceNielsen</dc:creator>
      <dc:date>2012-03-26T14:01:02Z</dc:date>
    </item>
  </channel>
</rss>

