2

I am trying to export attachments from fGDB and populate fields with the picture link path. I created a script following these instructions http://support.esri.com/en/technical-article/000014520

import arcpy
from arcpy import da
import os

arcpy.env.overwriteOutput = True
arcpy.env.workspace = r'C:\Users\vincent.law\Documents\POSTPROCESSING\22b0445e7384453293e817762843f540.gdb'

inTable = arcpy.ListTables("*ATTACH")
fileLocation = r'C:\Users\vincent.law\Documents\POSTPROCESSING\Pictures'

for tableattach in inTable:
    FCname = str(tableattach[:-8])
    arcpy.AddField_management (tableattach, "ATTACHMENTID1", "TEXT", "", "", "", "", "NULLABLE")
    arcpy.CalculateField_management (tableattach, "ATTACHMENTID1", "!ATTACHMENTID!", "Python_9.3") 

    with da.SearchCursor(tableattach, ['DATA', 'ATT_NAME', 'ATTACHMENTID']) as cursor:
        for item in cursor:
            attachment = item[0]
            filenum = FCname + "_" + "ATT" + str(item[2]) + "_"
            filename = filenum + str(item[1])
            open(fileLocation + os.sep + filename, 'wb').write(attachment.tobytes())
            del item
            del filenum
            del filename
            del attachment


arcpy.AddField_management ("Building", "Picture", "TEXT", "", "", "", "", "NULLABLE")
arcpy.MakeFeatureLayer_management ("Building", "Building_lyr")
arcpy.CalculateField_management ("Building_lyr", "Picture", "!Picture1!", "PYTHON_9.3")

arcpy.AddField_management ("Culvert_Line", "Picture", "TEXT", "", "", "", "", "NULLABLE")
arcpy.MakeFeatureLayer_management ("Culvert_Line", "Culvert_Line_lyr")
arcpy.CalculateField_management ("Culvert_Line_lyr", "Picture", "!Picture1!", "PYTHON_9.3")

arcpy.AddField_management ("Speed_Bump", "Picture", "TEXT", "", "", "", "", "NULLABLE")
arcpy.MakeFeatureLayer_management ("Speed_Bump", "Speed_Bump_lyr")
arcpy.CalculateField_management ("Speed_Bump_lyr", "Picture", "!Picture1!", "PYTHON_9.3")

arcpy.AddField_management ("Bridge", "Picture", "TEXT", "", "", "", "", "NULLABLE")
arcpy.MakeFeatureLayer_management ("Bridge", "Bridge_lyr")
arcpy.CalculateField_management ("Bridge_lyr", "Picture", "!Picture1!", "PYTHON_9.3")


gdb = arcpy.ListFeatureClasses()
gdb.remove('Boundary')
gdb.remove('Trails')


for fc in gdb:
    fcname1 = '"' + str(fc) + '"'
    arcpy.AddField_management(fc, "FCname", "TEXT","","","","", "NULLABLE", "NON_REQUIRED")
    arcpy.MakeFeatureLayer_management(fc, "layer1")
    arcpy.CalculateField_management("layer1", "FCname", fcname1, "PYTHON_9.3", "")

    attachtable = str(fc) + "__ATTACH"
    picturepath = "fileLocation + '\\' + '!FCname!' + '_' + 'ATT' + '!ATTACHMENTID1!' + '_' + '!ATT_NAME!'"


    arcpy.JoinField_management(fc, "GlobalID", attachtable, "REL_GLOBALID")
    arcpy.MakeFeatureLayer_management(fc, "layer","REL_GLOBALID IS NOT NULL")
    arcpy.CalculateField_management("layer","Picture", picturepath, "PYTHON_9.3")


arcpy.CalculateField_management ("Speed_Bump_lyr", "Picture1", "!Picture!", "PYTHON_9.3")
arcpy.CalculateField_management ("Building_lyr", "Picture1", "!Picture!", "PYTHON_9.3")
arcpy.CalculateField_management ("Culvert_Line_lyr", "Picture1", "!Picture!", "PYTHON_9.3")
arcpy.CalculateField_management ("Bridge_lyr", "Picture1", "!Picture!", "PYTHON_9.3")

arcpy.DeleteField_management("Building", "Picture")
arcpy.DeleteField_management("Culvert_Line", "Picture")
arcpy.DeleteField_management("Bridge", "Picture")
arcpy.DeleteField_management("Speed_Bump", "Picture")

I am getting the following error:

Traceback (most recent call last): File "C:\Python27\ArcGISx6410.3\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript exec codeObject in main.dict File "\goa\desktop\T_Z\vincent.law\Desktop\Script1.py", line 62, in arcpy.CalculateField_management("layer","Picture", picturepath, "PYTHON_9.3") File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\management.py", line 3457, in CalculateField raise e ExecuteError: ERROR 000539: SyntaxError: invalid syntax (, line 1)

I don't know what's wrong with the syntax for the following:

picturepath = "fileLocation + '\\' +'!FCname!' + '_' + 'ATT' + '!ATTACHMENTID1!' + '_' + '!ATT_NAME!'"

1 Answer 1

1

You can´t use the fieldnames this way (!FCname!). You have to use them like in your "with arcpy.da.searchcursor" block. The notation !Fieldname! is for the internal use in the arcmap fieldcalculator. Within the script these fields are normal variables.

The picturepath changes per attachment or file that you want to create. Now you have your picturepath in the loop for the featureclasses. But you need to iterate the rows within the table to get each ID for each attachment. Just put your file creation into your "arcpy.da.searchcursor" block. There everything is done to write a file for each attachment. And when you need to iterate over more feature-classes then you integreate this loop into your "for fc in gdb:" block

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.