RuntimeError when trying to download attachments using Python API

01-27-2022 09:08 AM
Occasional Contributor

I tried downloading attachments from an ArcGIS Online Feature Layer using the following code from within the Python Terminal in ArcGIS Pro:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
Created on Thu Jan 20 16:34:30 2022

@author: manuel
"""import os

## ArcGIS log-in
import arcgis, math
from arcgis.gis import GIS
gis = GIS(None, "*******", "*******", verify_cert = False)
ArcGIS_content = "My_survey"

## get ArcGIS Online attachments
search_result =, "Feature Layer")
item = search_result[0]
FeatureLayer = item.layers[0]
FeatureObjectIDs = FeatureLayer.query(where = "1=1", return_ids_only = True)

tmp_dir ="C:\\Users\\Manuel\\Desktop\\test\\"
for objID in FeatureObjectIDs["objectIds"]:
    attachments_list = FeatureLayer.attachments.get_list(oid = objID)
    if len(attachments_list) > 0:
        for attmID in range(1, len(attachments_list) + 1):
           obj_dir = os.path.join(tmp_dir, str(objID))
  = objID,\
               attachment_id = attmID,\
               save_path = obj_dir)

However, it gave me the following error after creating the first folder with attachments and subsequently creating the second folder:

RuntimeError Traceback (most recent call last)
In [94]:
Line 62: save_path = obj_dir)

File C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\arcgis\features\, in download:
Line 355: raise RuntimeError


I am not sure what to do with this error. The script creates one folder, downloads the images I want, then creates the next folder and returns this error. Doesn't make much sense to me. Is it about some connection timeout? How can I solve this issue?

Tags (2)
0 Kudos
1 Reply
Occasional Contributor

I found the attachment IDs are not just consecutive integers, but I had to use something like

attachment_IDs = [a["id"] for a in attachments_list]

0 Kudos