!PicForward!.split('\')[2]
Solved! Go to Solution.
import os Input = r'\\ITDNAS\MapLibrary\Photos\RRpics\058721Nn.jpg' filename = os.path.basename(Input)
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)
import os Input = r'\\ITDNAS\MapLibrary\Photos\RRpics\058721Nn.jpg' filename = os.path.basename(Input)
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)
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.
>>> import os >>> rows = arcpy.UpdateCursor("projected") >>> for row in rows: ... row.name = str(os.path.basename(row.path)) ... rows.updateRow(row) ... >>>
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]
>>> 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' >>>
!PicForward!.split('\\')[-1]
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'.