<?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: Escape chars - how to extract just file name? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/escape-chars-how-to-extract-just-file-name/m-p/717033#M55582</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Caleb,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You sold me!&amp;nbsp; Thanks for the help&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Yeah, that is strange indeed.&amp;nbsp; Another good reason not to use the field calculator!&amp;nbsp; I just copied and pasted your variable "\\ITDNAS\MapLibrary\Photos\RRpics\058721Nn.jp" into a field and ran this code in the python window of ArcMap and it worked perfectly.&amp;nbsp; I would switch to the cursor if I were you because that worked for me.&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;gt;&amp;gt;&amp;gt; import os
&amp;gt;&amp;gt;&amp;gt; rows = arcpy.UpdateCursor("projected")
&amp;gt;&amp;gt;&amp;gt; for row in rows:
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.name = str(os.path.basename(row.path))
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;gt;&amp;gt;&amp;gt; 
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;Here's a screenshot of the attribute table afterwards&lt;BR /&gt;&lt;BR /&gt;[ATTACH=CONFIG]24867[/ATTACH]&lt;BR /&gt;&lt;BR /&gt;and here is my python window in ArcMap&lt;BR /&gt;[ATTACH=CONFIG]24868[/ATTACH]&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 06:42:28 GMT</pubDate>
    <dc:creator>MichaelMiller2</dc:creator>
    <dc:date>2021-12-12T06:42:28Z</dc:date>
    <item>
      <title>Escape chars - how to extract just file name?</title>
      <link>https://community.esri.com/t5/python-questions/escape-chars-how-to-extract-just-file-name/m-p/717029#M55578</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have an attribute that contains the entire path of some photos and I would like to extract just the file name with extension.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Input:&amp;nbsp; \\ITDNAS\MapLibrary\Photos\RRpics\058721Nn.jpg&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Output: 058721Nn.jpg&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Attempting to use the following in the field calculator, but I'm getting hung up on the escape chars (\) in the path. &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;!PicForward!.split('\')[2]&lt;/PRE&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Could someone point out the correct way to go about this, please.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 31 May 2013 15:23:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/escape-chars-how-to-extract-just-file-name/m-p/717029#M55578</guid>
      <dc:creator>MichaelMiller2</dc:creator>
      <dc:date>2013-05-31T15:23:07Z</dc:date>
    </item>
    <item>
      <title>Re: Escape chars - how to extract just file name?</title>
      <link>https://community.esri.com/t5/python-questions/escape-chars-how-to-extract-just-file-name/m-p/717030#M55579</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You can use the os.path module to return the basename (filename).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import os

Input =&amp;nbsp; r'\\ITDNAS\MapLibrary\Photos\RRpics\058721Nn.jpg'
filename = os.path.basename(Input)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;the filename variable will return '058721Nn.jpg'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In this case it would be much better to use an update cursor than the field calculator. In fact, I don't ever use the field calculator because cursors are way easier.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy, os

fc = r'path\to\your\featureClass'
rows = arcpy.UpdateCursor(fc)
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; row.FieldToUpdate = str(os.path.basename(row.PicForward))
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Make sure to change the FieldToUpdate to your field name that you want to update.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 16:50:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/escape-chars-how-to-extract-just-file-name/m-p/717030#M55579</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-12T16:50:52Z</dc:date>
    </item>
    <item>
      <title>Re: Escape chars - how to extract just file name?</title>
      <link>https://community.esri.com/t5/python-questions/escape-chars-how-to-extract-just-file-name/m-p/717031#M55580</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Caleb,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for pointing out the os path syntax and using cursors suggestion. In this case I just wanted to use the field calculator in ArcMap and it is working somewhat using your solution. Problem is it's returning for example "RRpics 8712Pn.jpg" instead of "058712Pn.jpg", from "\\ITDNAS\MapLibrary\Photos\RRpics\058712Pn.jpg". Similar results for all records......kinda strange.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Screenshot of Field Calculator code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[ATTACH=CONFIG]24863[/ATTACH]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt; You can use the os.path module to return the basename (filename).&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt; &lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import os

Input =&amp;nbsp; r'\\ITDNAS\MapLibrary\Photos\RRpics\058721Nn.jpg'
filename = os.path.basename(Input)
&lt;/PRE&gt; &lt;BR /&gt; &lt;BR /&gt;the filename variable will return '058721Nn.jpg'&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt;In this case it would be much better to use an update cursor than the field calculator. In fact, I don't ever use the field calculator because cursors are way easier.&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt; &lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy, os

fc = r&lt;SPAN style="color:&amp;quot;#FF0000&amp;quot;;"&gt;'path\to\your\featureClass&lt;/SPAN&gt;'
rows = arcpy.UpdateCursor(fc)
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; row.&lt;SPAN style="color:&amp;quot;#FF0000&amp;quot;;"&gt;FieldToUpdate&lt;/SPAN&gt; = str(os.path.basename(row.PicForward))
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
&lt;/PRE&gt; &lt;BR /&gt; &lt;BR /&gt;Make sure to change the FieldToUpdate to your field name that you want to update.&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 06:42:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/escape-chars-how-to-extract-just-file-name/m-p/717031#M55580</guid>
      <dc:creator>MichaelMiller2</dc:creator>
      <dc:date>2021-12-12T06:42:22Z</dc:date>
    </item>
    <item>
      <title>Re: Escape chars - how to extract just file name?</title>
      <link>https://community.esri.com/t5/python-questions/escape-chars-how-to-extract-just-file-name/m-p/717032#M55581</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Yeah, that is strange indeed.&amp;nbsp; Another good reason not to use the field calculator!&amp;nbsp; I just copied and pasted your variable "\\ITDNAS\MapLibrary\Photos\RRpics\058721Nn.jp" into a field and ran this code in the python window of ArcMap and it worked perfectly.&amp;nbsp; I would switch to the cursor if I were you because that worked for me.&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; import os
&amp;gt;&amp;gt;&amp;gt; rows = arcpy.UpdateCursor("projected")
&amp;gt;&amp;gt;&amp;gt; for row in rows:
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.name = str(os.path.basename(row.path))
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;gt;&amp;gt;&amp;gt; 
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's a screenshot of the attribute table afterwards&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[ATTACH=CONFIG]24867[/ATTACH]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;and here is my python window in ArcMap&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[ATTACH=CONFIG]24868[/ATTACH]&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 06:42:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/escape-chars-how-to-extract-just-file-name/m-p/717032#M55581</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-12T06:42:25Z</dc:date>
    </item>
    <item>
      <title>Re: Escape chars - how to extract just file name?</title>
      <link>https://community.esri.com/t5/python-questions/escape-chars-how-to-extract-just-file-name/m-p/717033#M55582</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Caleb,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You sold me!&amp;nbsp; Thanks for the help&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Yeah, that is strange indeed.&amp;nbsp; Another good reason not to use the field calculator!&amp;nbsp; I just copied and pasted your variable "\\ITDNAS\MapLibrary\Photos\RRpics\058721Nn.jp" into a field and ran this code in the python window of ArcMap and it worked perfectly.&amp;nbsp; I would switch to the cursor if I were you because that worked for me.&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;gt;&amp;gt;&amp;gt; import os
&amp;gt;&amp;gt;&amp;gt; rows = arcpy.UpdateCursor("projected")
&amp;gt;&amp;gt;&amp;gt; for row in rows:
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.name = str(os.path.basename(row.path))
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;gt;&amp;gt;&amp;gt; 
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;Here's a screenshot of the attribute table afterwards&lt;BR /&gt;&lt;BR /&gt;[ATTACH=CONFIG]24867[/ATTACH]&lt;BR /&gt;&lt;BR /&gt;and here is my python window in ArcMap&lt;BR /&gt;[ATTACH=CONFIG]24868[/ATTACH]&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 06:42:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/escape-chars-how-to-extract-just-file-name/m-p/717033#M55582</guid>
      <dc:creator>MichaelMiller2</dc:creator>
      <dc:date>2021-12-12T06:42:28Z</dc:date>
    </item>
    <item>
      <title>Re: Escape chars - how to extract just file name?</title>
      <link>https://community.esri.com/t5/python-questions/escape-chars-how-to-extract-just-file-name/m-p/717034#M55583</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If using 10.1, the arcpy.da.walk function will give you the dirpath, dirnames, and filenames of all FC/files in a workspace that match your filter:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//018w00000023000000" rel="nofollow noopener noreferrer" target="_blank"&gt;http://resources.arcgis.com/en/help/main/10.1/index.html#//018w00000023000000&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Otherwise, you can use os.path.basename to get these:&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; Input =&amp;nbsp; r'\\ITDNAS\MapLibrary\Photos\RRpics\058721Nn.jpg'
&amp;gt;&amp;gt;&amp;gt; filename = os.path.basename(Input)
&amp;gt;&amp;gt;&amp;gt; os.path.basename(os.path.dirname(Input))
'RRpics'
&amp;gt;&amp;gt;&amp;gt; os.path.basename(os.path.dirname(os.path.dirname(Input)))
'Photos'
&amp;gt;&amp;gt;&amp;gt; filename
'058721Nn.jpg'
&amp;gt;&amp;gt;&amp;gt; &lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;R_&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 06:42:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/escape-chars-how-to-extract-just-file-name/m-p/717034#M55583</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2021-12-12T06:42:31Z</dc:date>
    </item>
    <item>
      <title>Re: Escape chars - how to extract just file name?</title>
      <link>https://community.esri.com/t5/python-questions/escape-chars-how-to-extract-just-file-name/m-p/717035#M55584</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I like cursors too, but your original code was actually almost there. Rember that the '\\' is the Python path delimiter in Windows, and an index of [-1] will grab the last item.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;!PicForward!.split('\\')[-1]&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;so regardless of how many folders deep your file is, if the field value is 'C:\temp\myimage.jpg', the code above will return 'myimage.jpg'.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 03 Jun 2013 22:20:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/escape-chars-how-to-extract-just-file-name/m-p/717035#M55584</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2013-06-03T22:20:30Z</dc:date>
    </item>
    <item>
      <title>Re: Escape chars - how to extract just file name?</title>
      <link>https://community.esri.com/t5/python-questions/escape-chars-how-to-extract-just-file-name/m-p/717036#M55585</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Chris, appreciate the tip!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I like cursors too, but your original code was actually almost there. Rember that the '\\' is the Python path delimiter in Windows, and an index of [-1] will grab the last item.&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;!PicForward!.split('\\')[-1]&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;so regardless of how many folders deep your file is, if the field value is 'C:\temp\myimage.jpg', the code above will return 'myimage.jpg'.&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 04 Jun 2013 13:00:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/escape-chars-how-to-extract-just-file-name/m-p/717036#M55585</guid>
      <dc:creator>MichaelMiller2</dc:creator>
      <dc:date>2013-06-04T13:00:09Z</dc:date>
    </item>
  </channel>
</rss>

