!PicForward!.split('\')[2]
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)
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'.
!PicForward!.split('\\')[-1]
>>> 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' >>>
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]
>>> import os >>> rows = arcpy.UpdateCursor("projected") >>> for row in rows: ... row.name = str(os.path.basename(row.path)) ... 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.
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.