0

I'm using the following python script to export photo attachments:

from arcpy import da
import os

attachTable = arcpy.GetParameterAsText(0)
origTable = arcpy.GetParameterAsText(1)
nameField = arcpy.GetParameterAsText(2)
fileLocation = arcpy.GetParameterAsText(3)
with da.SearchCursor(attachTable,['DATA','ATT_NAME','REL_GLOBALID']) as attachCursor:
    for attRow in attachCursor:
        binaryRep = attRow[0]
        fileName = attRow[1]
        relID = attRow[2]

with da.SearchCursor(origTable,['GlobalID', nameField]) as originCursor:
    for origRow in originCursor:
        origID = origRow[0]
        origName = origRow[1]
        if origID == relID:
            break
        open(fileLocation + os.sep + origName + ".jpg", 'wb').write(binaryRep.tobytes())
        del origRow

I get this error after running it:

Runtime error

Traceback (most recent call last): File "", line 7, in IOError: [Errno 2] No such file or directory: u'W:\work\dry_creek_exp\images\Hwy 20/Crowley Rd (turn left).jpg'

... I believe this file doesn't exist because I didn't have a photo attached to it. My question is, does anyone know of a way to get python to ignore these files that don't have attachments?? I hoping when I save this code as a script and run it on bigger projects that it will help the run time.

  • Are you creating the fileLocation directory somewhere in your code or does this directory already exist? W:\work\dry_creek_exp\images\Hwy 20 – artwork21 Jul 14 '17 at 19:28
  • No I'm not. Images is a folder that already exist, where I want the attachments to exported too. Hwy 20/Crowley Rd (turn left) is a feature that does not have a photo attached to it, so Hwy 20/Crowley Rd (turn left).jpg , the attachment, doesn't and can't exist. – Kiara Bailey Jul 14 '17 at 19:38
  • Does the Hwy 20 folder already exist or are you creating this in some other place in your script? – artwork21 Jul 14 '17 at 19:47
  • There is no file, folder, or directory with that name and I'm not creating one in my script. I edited my question to include the entire script. – Kiara Bailey Jul 14 '17 at 20:09
1

You can add some additional nested if conditions to check for values before the open method. Also you do not have to del the row object because you are using the with loop:

with da.SearchCursor(attachTable,['DATA','ATT_NAME','REL_GLOBALID']) as attachCursor:
    for attRow in attachCursor:
        if attRow[0]:
            binaryRep = attRow[0]
            if attRow[1]:
                fileName = attRow[1]
                if attRow[2]:
                    relID = attRow[2]

with da.SearchCursor(origTable,['GlobalID', nameField]) as originCursor:
    for origRow in originCursor:
        if origRow[0]:
            origID = origRow[0]
            if origRow[1]:
                origName = origRow[1]
                    if origID <> relID:
                        open(fileLocation + os.sep + origName + ".jpg", 'wb').write(binaryRep.tobytes())

Currently your cursor logic is going through the entire attachTable then cursoring through the origTable. With this structure you may be overwriting the binaryRep, fileName, and relID variables with only the last instance/record found that have these values. You may need to nest the second cursor within the first cursor after the relID = attRow[2] statement to reference all found values within the attachTable.

  • I copied your example and nested the second cursor as you suggested. I don't get any errors, nothing actually happens or at least the attachments are appearing in the folder I specified. Though i did change the position of if origID <> relID: to if origID ==relID: and then added the break command – Kiara Bailey Jul 14 '17 at 22:21
  • @KiaraBailey, so you want to write to the jpg if origID <> relID? You may want to put some arcpy.AddMessage() statements to show the variables values and also to indicate where the current code is at to help you debug why the open statement is not being called. – artwork21 Jul 15 '17 at 13:20
  • ah! I'm sorry, your entire message pretty much just went over my head. Could you break that down for me please? – Kiara Bailey Jul 17 '17 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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