Escape chars - how to extract just file name?

880
7
Jump to solution
05-31-2013 08:23 AM
MichaelMiller2
Occasional Contributor III
I have an attribute that contains the entire path of some photos and I would like to extract just the file name with extension.

Example:
Input:  \\ITDNAS\MapLibrary\Photos\RRpics\058721Nn.jpg
Output: 058721Nn.jpg

Attempting to use the following in the field calculator, but I'm getting hung up on the escape chars (\) in the path.
!PicForward!.split('\')[2]
    

Could someone point out the correct way to go about this, please.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
You can use the os.path module to return the basename (filename).

import os

Input =  r'\\ITDNAS\MapLibrary\Photos\RRpics\058721Nn.jpg'
filename = os.path.basename(Input)


the filename variable will return '058721Nn.jpg'

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.

import arcpy, os

fc = r'path\to\your\featureClass'
rows = arcpy.UpdateCursor(fc)
for row in rows:
    row.FieldToUpdate = str(os.path.basename(row.PicForward))
    rows.updateRow(row)


Make sure to change the FieldToUpdate to your field name that you want to update.

View solution in original post

0 Kudos
7 Replies
by Anonymous User
Not applicable
You can use the os.path module to return the basename (filename).

import os

Input =  r'\\ITDNAS\MapLibrary\Photos\RRpics\058721Nn.jpg'
filename = os.path.basename(Input)


the filename variable will return '058721Nn.jpg'

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.

import arcpy, os

fc = r'path\to\your\featureClass'
rows = arcpy.UpdateCursor(fc)
for row in rows:
    row.FieldToUpdate = str(os.path.basename(row.PicForward))
    rows.updateRow(row)


Make sure to change the FieldToUpdate to your field name that you want to update.
0 Kudos
MichaelMiller2
Occasional Contributor III
Caleb,

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.

Screenshot of Field Calculator code:
[ATTACH=CONFIG]24863[/ATTACH]

You can use the os.path module to return the basename (filename). 

import os

Input =  r'\\ITDNAS\MapLibrary\Photos\RRpics\058721Nn.jpg'
filename = os.path.basename(Input)


the filename variable will return '058721Nn.jpg' 

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. 

import arcpy, os

fc = r'path\to\your\featureClass'
rows = arcpy.UpdateCursor(fc)
for row in rows:
    row.FieldToUpdate = str(os.path.basename(row.PicForward))
    rows.updateRow(row)


Make sure to change the FieldToUpdate to your field name that you want to update.
0 Kudos
by Anonymous User
Not applicable
Yeah, that is strange indeed.  Another good reason not to use the field calculator!  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.  I would switch to the cursor if I were you because that worked for me.

>>> import os
>>> rows = arcpy.UpdateCursor("projected")
>>> for row in rows:
...     row.name = str(os.path.basename(row.path))
...     rows.updateRow(row)
...     
>>> 


Here's a screenshot of the attribute table afterwards

[ATTACH=CONFIG]24867[/ATTACH]

and here is my python window in ArcMap
[ATTACH=CONFIG]24868[/ATTACH]
0 Kudos
MichaelMiller2
Occasional Contributor III
Caleb,

You sold me!  Thanks for the help


Yeah, that is strange indeed.  Another good reason not to use the field calculator!  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.  I would switch to the cursor if I were you because that worked for me.

>>> import os
>>> rows = arcpy.UpdateCursor("projected")
>>> for row in rows:
...     row.name = str(os.path.basename(row.path))
...     rows.updateRow(row)
...     
>>> 


Here's a screenshot of the attribute table afterwards

[ATTACH=CONFIG]24867[/ATTACH]

and here is my python window in ArcMap
[ATTACH=CONFIG]24868[/ATTACH]
0 Kudos
RhettZufelt
MVP Frequent Contributor
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:

http://resources.arcgis.com/en/help/main/10.1/index.html#//018w00000023000000

Otherwise, you can use os.path.basename to get these:

>>> Input =  r'\\ITDNAS\MapLibrary\Photos\RRpics\058721Nn.jpg'
>>> filename = os.path.basename(Input)
>>> os.path.basename(os.path.dirname(Input))
'RRpics'
>>> os.path.basename(os.path.dirname(os.path.dirname(Input)))
'Photos'
>>> filename
'058721Nn.jpg'
>>> 


R_
0 Kudos
ChrisSnyder
Regular Contributor III
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.

!PicForward!.split('\\')[-1]


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'.
0 Kudos
MichaelMiller2
Occasional Contributor III
Thanks Chris, appreciate the tip!


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.

!PicForward!.split('\\')[-1]


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'.
0 Kudos