<?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: Replace file names in a folder in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/replace-file-names-in-a-folder/m-p/50893#M4026</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Natalie, you are going to have to add the path even though you set the workspace (which only Arc* cares about)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For example&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; f &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; os&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;listdir&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;input_folder&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    &lt;SPAN class="keyword token"&gt;if&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'arr'&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; f&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
        old &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"{}\\{}"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;format&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;input_folder&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; f&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
        os&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;rename&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;old&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; old&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;replace&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'arr'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'arrnew'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Try a few files in C:\temp!, your paths are still .... (see my recently edited blog &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/wink.png" /&gt;&amp;nbsp;&lt;A href="https://community.esri.com/blogs/dan_patterson/2016/08/14/filenames-and-file-paths-in-python" target="_blank"&gt;/blogs/dan_patterson/2016/08/14/filenames-and-file-paths-in-python&lt;/A&gt;&amp;nbsp; and check your flotsam)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It renamed a bunch of text files that contained 'arr' in them&lt;/P&gt;&lt;P&gt;Line 3 is the workspace folder (like you did), it just safely appends the existing filename 'f' to the path to create 'old'&lt;/P&gt;&lt;P&gt;Then os.rename takes 'old' and replaced 'arr' with 'arrnew'&lt;/P&gt;&lt;P&gt;Hope you get the drift I hope&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 10 Dec 2021 21:58:10 GMT</pubDate>
    <dc:creator>DanPatterson_Retired</dc:creator>
    <dc:date>2021-12-10T21:58:10Z</dc:date>
    <item>
      <title>Replace file names in a folder</title>
      <link>https://community.esri.com/t5/python-questions/replace-file-names-in-a-folder/m-p/50890#M4023</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; arcpy
&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; os

arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;env&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;workspace &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; r&lt;SPAN class="string token"&gt;"C:\Users\natb0\OneDrive - drycreekeng.com\GIS\GIS\Data\Dry_Creek\TerrAvion_Imagery\062319"&lt;/SPAN&gt;
input_folder &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;env&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;workspace

&lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; p &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; os&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;listdir&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;input_folder&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    p&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;replace&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"CIR_4326"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"RGB_062319"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    p&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;replace&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"TIRS_T_4326"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"Temp_062319"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    p&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;replace&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"NDVI_NDVI_2_4326"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"NDVI_062319"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    &lt;SPAN class="keyword token"&gt;print&lt;/SPAN&gt; p‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I have to download 48 images from a website weekly, and I would like to write a script to rename the files to something more user friendly and understandable.&amp;nbsp; For each farm field (16 fields total), I download 3 images (#farmfield_TIRS_T_4326.tiff, #farmfield_CIR_4326.tiff, and #farmfield_NDVI_NDVI_2_4326.tiff), where TIRS = temperature, CIR = infrared, and NDVI is NDVI. For each file name, I would like to replace "&lt;SPAN style="display: inline !important; float: none; background-color: #ffffff; color: #3d3d3d; font-family: Helvetica Neue,Helvetica,Arial,Lucida Grande,sans-serif; font-size: 15px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; overflow-wrap: break-word; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;TIRS_T_4326&lt;/SPAN&gt;" with "Temp_062319",&amp;nbsp; "&lt;SPAN style="display: inline !important; float: none; background-color: #ffffff; color: #3d3d3d; font-family: Helvetica Neue,Helvetica,Arial,Lucida Grande,sans-serif; font-size: 15px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; overflow-wrap: break-word; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;CIR_4326&lt;/SPAN&gt;" with "RGB_062319", and "&lt;SPAN style="display: inline !important; float: none; background-color: #ffffff; color: #3d3d3d; font-family: Helvetica Neue,Helvetica,Arial,Lucida Grande,sans-serif; font-size: 15px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; overflow-wrap: break-word; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;NDVI_NDVI_2_4326" with "NDVI_062319"&lt;/SPAN&gt;.&amp;nbsp; When I run the code, it runs fine but the file names do not change.&amp;nbsp; So Im guessing .replace is the wrong argument.&amp;nbsp; Does anyone know what I should use instead? My goal is to do a find a replace on the file names.&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:58:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-file-names-in-a-folder/m-p/50890#M4023</guid>
      <dc:creator>DryCreekEng</dc:creator>
      <dc:date>2021-12-10T21:58:08Z</dc:date>
    </item>
    <item>
      <title>Re: Replace file names in a folder</title>
      <link>https://community.esri.com/t5/python-questions/replace-file-names-in-a-folder/m-p/50891#M4024</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Natalie&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/blogs/dan_patterson/2016/08/14/script-formatting"&gt;/blogs/dan_patterson/2016/08/14/script-formatting&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;so we can comment with line numbers.&lt;/P&gt;&lt;P&gt;Could you provide information beyond 'but I know it is wrong'&amp;nbsp; Does it now do what you want? are there errors?&lt;/P&gt;&lt;P&gt;Sample of errors or print statements.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Also, OneDrive and other non-local drives can be sketchy and folders that begin with numbers can be an issue as well.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 25 Jun 2019 23:05:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-file-names-in-a-folder/m-p/50891#M4024</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2019-06-25T23:05:36Z</dc:date>
    </item>
    <item>
      <title>Re: Replace file names in a folder</title>
      <link>https://community.esri.com/t5/python-questions/replace-file-names-in-a-folder/m-p/50892#M4025</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks for your input as always.&amp;nbsp; I updated my question per your recommendations.&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 25 Jun 2019 23:23:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-file-names-in-a-folder/m-p/50892#M4025</guid>
      <dc:creator>DryCreekEng</dc:creator>
      <dc:date>2019-06-25T23:23:42Z</dc:date>
    </item>
    <item>
      <title>Re: Replace file names in a folder</title>
      <link>https://community.esri.com/t5/python-questions/replace-file-names-in-a-folder/m-p/50893#M4026</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Natalie, you are going to have to add the path even though you set the workspace (which only Arc* cares about)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For example&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; f &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; os&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;listdir&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;input_folder&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    &lt;SPAN class="keyword token"&gt;if&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'arr'&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; f&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
        old &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"{}\\{}"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;format&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;input_folder&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; f&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
        os&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;rename&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;old&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; old&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;replace&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'arr'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'arrnew'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Try a few files in C:\temp!, your paths are still .... (see my recently edited blog &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/wink.png" /&gt;&amp;nbsp;&lt;A href="https://community.esri.com/blogs/dan_patterson/2016/08/14/filenames-and-file-paths-in-python" target="_blank"&gt;/blogs/dan_patterson/2016/08/14/filenames-and-file-paths-in-python&lt;/A&gt;&amp;nbsp; and check your flotsam)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It renamed a bunch of text files that contained 'arr' in them&lt;/P&gt;&lt;P&gt;Line 3 is the workspace folder (like you did), it just safely appends the existing filename 'f' to the path to create 'old'&lt;/P&gt;&lt;P&gt;Then os.rename takes 'old' and replaced 'arr' with 'arrnew'&lt;/P&gt;&lt;P&gt;Hope you get the drift I hope&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:58:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-file-names-in-a-folder/m-p/50893#M4026</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-10T21:58:10Z</dc:date>
    </item>
  </channel>
</rss>

