0

I found this code on a youtube tutorial (https://www.youtube.com/watch?v=u26Kpe4NBqU) for batching exported attachments from arcgis online.

from arcpy import da
import os

attachTable = arcpy.GetParameterAsText(0)
origTable = arcpy.GetParameterAsText(1)
nameField = arcpy.GetParameterAsText(2)
fileLocation= arcpy.GetParameterAsText(3)

attachCursor = da.SearchCursor(attachTable,['DATA', 'ATT_NAME','REL_GLOBALID'])

for attRow in attachCursor:
    binaryRep = attRow[0]
    fileName = attRow[1]
    relID = attRow[2]

    originCursor = da.SearchCursor(origTable, ['GLOBALID', nameField])

    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 originCursor

BUT when I enter the script into python, I get an error here:

attachCursor = da.SearchCursor(attachTable,['DATA','ATT_NAME','REL_GLOBALID'])
Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
RuntimeError: cannot open ''*

Does anyone know why I receive this error?

I think it has something to do with the Cursor (either attach or search), but I'm stuck and can only scratch my head in confusion.

... I've also tried to enter it as a script, when I do it will run but usually never to completion. It doesn't crash but just keeps going. The one time it worked took an hour for a handful of attachments and though it says it was successful, I couldn't find the exported files. I don't have enough reputation points to add all the steps I took as a pictures so here is a link to a pdf with screenshots:

link to steps taken to create and run script

  • 1
    What values are being passed to your script from your tool dialog by the four tool parameters? I recommend hardwiring these while testing and presenting code here. – PolyGeo Jul 6 '17 at 1:12
  • parameter(0) is the attachment table, (1) is the feature class, (2) is what attribute you want the exports to named after, (3) is the output location – Kiara Bailey Jul 6 '17 at 16:08
  • 1
    That's what you are using your parameters for but what are the actual values being passed on one run when you see this error? – PolyGeo Jul 6 '17 at 18:59
  • I'm not sure I really understand your question. I'm very new to all this, but I wasn't putting in any specific values into the code itself, because I don't want to run it just once, but want other people in my office to be able to use it in the future. So when the script didn't work, I wanted to find out why/where it was running into a problem and so entered it into python – Kiara Bailey Jul 7 '17 at 14:45
1

You have not imported arcpy

import arcpy
from arcpy import da
import os

attachTable = arcpy.GetParameterAsText(0)

Actually after a bit more testing, it appears to me that you haven't provided a parameter for attachTable = arcpy.GetParameterAsText(0). If I don't provide a value here I get the exact same error. If I provide a valid value I get no error.

Check that you are passing a valid feature class or layer path through from your script tool when you run it.

| improve this answer | |
  • I"m pretty sure the feature class is valid, when I run it as a script rather than entering it into python, the drop down lets me choose which feature class I wish to choose from. How would I go about providing a parameter for attachTable = arcpy.GetParameterAsText(0) – Kiara Bailey Jul 6 '17 at 16:10
  • @KiaraBailey Have you created a script tool in ArcToolbox? You set your parameters there, and then when running the tool you populate the fields and these are passed to the script by the tool. – Midavalo Jul 6 '17 at 16:59
  • @KiaraBailey I think you need to edit your question to include the details about your script tool and how you've set it up in ArcToolbox. – Midavalo Jul 6 '17 at 17:42
  • I entered it as a script by saving it in a notepad, creating a toolbox, and adding the script. When I added the script, I entered a name, label, and description and check the box to store relative path names. Then I browsed for and added the file. Next I entered the Display Names& Data Types: Attachment Table- Table, Feature Table- Feature Layer, Field for Naming - Field (obtained from parameter(1)), Output Location- Folder. Saved everything. Opened up the script, entered the parameters, and ran it. It doesn't crash, but it never finishes runnning. – Kiara Bailey Jul 6 '17 at 18:34
  • @KiaraBailey Please edit your question to add this info and some screenshots to show how you have set up your script tool. Your parameters (passed to arcpy.GetParametersAsText()) come from the script tool you create, so if that's not working the problem may be related to how you've configured your tool. – Midavalo Jul 6 '17 at 18:38

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.