<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Using AttachmentManager to Download Attachments Saves in Separate Folders in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/using-attachmentmanager-to-download-attachments/m-p/1075762#M6323</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/482199"&gt;@cschooley&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have a look at this tool I created - might be helpful:&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/t5/arcgis-pro-blog/export-attachments-toolbox/ba-p/1022018" target="_blank"&gt;https://community.esri.com/t5/arcgis-pro-blog/export-attachments-toolbox/ba-p/1022018&lt;/A&gt;&lt;/P&gt;&lt;H6&gt;If this answer solved your question or if you found it helpful please mark it accordingly to help others who have the same question.&lt;/H6&gt;</description>
    <pubDate>Tue, 06 Jul 2021 06:12:02 GMT</pubDate>
    <dc:creator>UriGilad_EsriAu</dc:creator>
    <dc:date>2021-07-06T06:12:02Z</dc:date>
    <item>
      <title>Using AttachmentManager to Download Attachments Saves in Separate Folders</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/using-attachmentmanager-to-download-attachments/m-p/1061943#M6128</link>
      <description>&lt;P&gt;The issue I'm having is with the &lt;A href="https://developers.arcgis.com/python/api-reference/arcgis.features.managers.html#attachmentmanager" target="_blank" rel="noopener"&gt;AttachmentManager&lt;/A&gt; module. I wrote a quick code to download all of the attachments for a feature layer stored on ArcGIS online.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;############# Get logged in and set up ###############

from arcgis.gis import GIS
PortalUrl = "https://www.arcgis.com"
username = ""
password = ""

FeatureLayerID = "e5c2bdc5755e4e21ab578550f2fa12a3"

SavePath= r"C:\Users\cschooley\Documents\Work\Heritage\Photos"

################# End Set-up #######################

if username == "" and password == "":
    gis = GIS()
else:
    gis = GIS(PortalUrl, username, password)

FeatureLayer = gis.content.get(FeatureLayerID)

layer = FeatureLayer.layers[0]

layer.attachments.download(oid = 1, save_path=SavePath)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It works just fine, but the issue I am having is that when it saves into the path I created, it creates a new folder for the ObjectID of that feature and the AttachmentID of that feature, nesting my photos behind two of their own folders.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;['C:\\Users\\cschooley\\Documents\\Work\\Heritage\\Photos\\1\\18\\2015-04-11 10.35.42.jpg']&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P data-unlink="true"&gt;I am wondering if there is a way to work around this. I would like to have the photos save directly into the 'photos' folder without the creation of the other folders.&amp;nbsp;&lt;/P&gt;&lt;P data-unlink="true"&gt;I suppose using the os module could work, but that's a little over my head. I found &lt;A href="https://community.esri.com/t5/arcgis-online-documents/downloading-feature-layer-attachments-via-the-arcgis-api-for/ta-p/916538" target="_blank" rel="noopener"&gt;this&lt;/A&gt;&amp;nbsp;&amp;nbsp;article which has been kind of helpful, but I couldn't get his code to work for me, which is why I wrote this one above. It was simpler and didn't have the extra stuff I didn't need. I only need to download the photos, not necessarily have all the information (ObjectIDs, etc.) attached to them.&amp;nbsp;&lt;/P&gt;&lt;P data-unlink="true"&gt;Thank you.&amp;nbsp;&lt;/P&gt;&lt;P data-unlink="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;P data-unlink="true"&gt;P.S. The feature ID used in the code is a public one, not the one I'm actually trying to use since that one is protected within my organization. It's from the &lt;A href="https://services.arcgis.com/jIL9msH9OI208GCb/ArcGIS/rest/services/JoshuaTreeHike/FeatureServer" target="_blank" rel="noopener"&gt;JoshuaTreeHike feature server.&lt;/A&gt;&amp;nbsp;It's the same set-up as mine though, where the Attachment IDs don't necessarily match the ObjectIDs.&amp;nbsp;&lt;/P&gt;&lt;P data-unlink="true"&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 May 2021 17:52:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/using-attachmentmanager-to-download-attachments/m-p/1061943#M6128</guid>
      <dc:creator>cschooley</dc:creator>
      <dc:date>2021-05-26T17:52:42Z</dc:date>
    </item>
    <item>
      <title>Re: Using AttachmentManager to Download Attachments Saves in Separate Folders</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/using-attachmentmanager-to-download-attachments/m-p/1062194#M6130</link>
      <description>&lt;P&gt;Hi cshooley,&lt;/P&gt;&lt;P&gt;Maybe you can used this workaround with os and shutil module to copy jpg after downloaded at Photos dirname as below :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;############# Get logged in and set up ###############
import os
import shutil
from arcgis.gis import GIS
PortalUrl = "https://www.arcgis.com"
username = ""
password = ""
FeatureLayerID = "e5c2bdc5755e4e21ab578550f2fa12a3"
SavePath= r"C:\Users\cschooley\Documents\Work\Heritage\Photos"

################# End Set-up #######################
if username == "" and password == "":
    gis = GIS()
else:
    gis = GIS(PortalUrl, username, password)
FeatureLayer = gis.content.get(FeatureLayerID)
layer = FeatureLayer.layers[0]
layer.attachments.download(oid = 1, save_path=SavePath)

################# Copy image #######################
for root, dirs, files in os.walk(SavePath):
    for f in files:
        if f.endswith('.jpg'):
            print("Copy jpg: {0}".format(str(f)))
            pathOriginal = os.path.join(root,f)
            pathTarget = os.path.join(SavePath,f)
            shutil.copyfile(pathOriginal, pathTarget)

### remove folders into Photos if you want
##for d in os.listdir(SavePath):
##    if os.path.isdir(os.path.join(SavePath,d)):
##        shutil.rmtree(os.path.join(SavePath,d))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this help you,&lt;/P&gt;&lt;P&gt;Best regards,&lt;/P&gt;&lt;P&gt;Fred&lt;/P&gt;</description>
      <pubDate>Thu, 27 May 2021 08:09:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/using-attachmentmanager-to-download-attachments/m-p/1062194#M6130</guid>
      <dc:creator>FrédéricPRALLY</dc:creator>
      <dc:date>2021-05-27T08:09:31Z</dc:date>
    </item>
    <item>
      <title>Re: Using AttachmentManager to Download Attachments Saves in Separate Folders</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/using-attachmentmanager-to-download-attachments/m-p/1062326#M6131</link>
      <description>&lt;P&gt;Thank you! That worked great. I had figured out how to get a list of all my files with this code:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;list_dir = []
for root, dirs, files, in os.walk(SavePath):
    for file in files:
        list_dir.append(file)&lt;/LI-CODE&gt;&lt;P&gt;But couldn't get any further than that. Now I think I can use your&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for root, dirs, files in os.walk(SavePath):
    for f in files:
        if f.endswith('.jpg'):
            print(r"Copy jpg: {0}")
            PathOriginal = os.path.join(root,f)
            PathTarget = os.path.join(SavePath, f)
            shutil.copyfile(PathOriginal, PathTarget)&lt;/LI-CODE&gt;&lt;P&gt;To get all the files, not just jpegs. Something like this:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for root, dirs, files in os.walk(SavePath):
    for f in files:
       print("Copy jpg: {0}".format(str(f))
       PathOriginal = os.path.join(root,f)
       PathTarget = os.path.join(SavePath, f)
       shutil.copyfile(PathOriginal, PathTarget)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again!&lt;/P&gt;</description>
      <pubDate>Thu, 27 May 2021 14:35:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/using-attachmentmanager-to-download-attachments/m-p/1062326#M6131</guid>
      <dc:creator>cschooley</dc:creator>
      <dc:date>2021-05-27T14:35:15Z</dc:date>
    </item>
    <item>
      <title>Re: Using AttachmentManager to Download Attachments Saves in Separate Folders</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/using-attachmentmanager-to-download-attachments/m-p/1062356#M6132</link>
      <description>&lt;P&gt;A secondary issue, and I don't know if you can help me here, but if some photos have the same name, there should be a way to rename the doubles right? So that they don't overwrite each other?&lt;/P&gt;</description>
      <pubDate>Thu, 27 May 2021 15:24:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/using-attachmentmanager-to-download-attachments/m-p/1062356#M6132</guid>
      <dc:creator>cschooley</dc:creator>
      <dc:date>2021-05-27T15:24:16Z</dc:date>
    </item>
    <item>
      <title>Re: Using AttachmentManager to Download Attachments Saves in Separate Folders</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/using-attachmentmanager-to-download-attachments/m-p/1062361#M6133</link>
      <description>&lt;P&gt;Best way is to check if jpg name exist before copy it.&lt;/P&gt;&lt;P&gt;See &lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/get-started/checking-for-existence.htm" target="_blank"&gt;https://pro.arcgis.com/en/pro-app/latest/arcpy/get-started/checking-for-existence.htm&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 27 May 2021 15:30:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/using-attachmentmanager-to-download-attachments/m-p/1062361#M6133</guid>
      <dc:creator>FrédéricPRALLY</dc:creator>
      <dc:date>2021-05-27T15:30:56Z</dc:date>
    </item>
    <item>
      <title>Re: Using AttachmentManager to Download Attachments Saves in Separate Folders</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/using-attachmentmanager-to-download-attachments/m-p/1062372#M6134</link>
      <description>&lt;P&gt;Right. So, that checks if something exists, but doesn't help with the issue of the shutil.copy overwriting if something exists. I'm downloading attachments from a feature layer where the attachments on different layers have the same name, but aren't the same picture and I want all of the pictures.&amp;nbsp;&lt;/P&gt;&lt;P&gt;From my reading I need to add something kind of like this to rename them before they save:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;while os.path.exists(f + "_" + str(i)):
     i+=i&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Not that exactly of course, but to add a number or something on the end of it so it doesn't overwrite the previous image.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 May 2021 15:46:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/using-attachmentmanager-to-download-attachments/m-p/1062372#M6134</guid>
      <dc:creator>cschooley</dc:creator>
      <dc:date>2021-05-27T15:46:34Z</dc:date>
    </item>
    <item>
      <title>Re: Using AttachmentManager to Download Attachments Saves in Separate Folders</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/using-attachmentmanager-to-download-attachments/m-p/1062764#M6142</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Try complete script below:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;############# Get logged in and set up ###############
import os
import shutil
from arcgis.gis import GIS
PortalUrl = "https://www.arcgis.com"
username = ""
password = ""
FeatureLayerID = "e5c2bdc5755e4e21ab578550f2fa12a3"
SavePath= r"C:\Users\cschooley\Documents\Work\Heritage\Photos"

################# End Set-up #######################
if username == "" and password == "":
    gis = GIS()
else:
    gis = GIS(PortalUrl, username, password)
FeatureLayer = gis.content.get(FeatureLayerID)
layer = FeatureLayer.layers[0]
layer.attachments.download(oid = 1, save_path=SavePath)

################# Copy image #######################
for root, dirs, files in os.walk(SavePath):
    for f in files:
        if f.endswith('.jpg'):
            print("Copy jpg: {0}".format(str(f)))
            pathOriginal = os.path.join(root,f)
            pathTarget = os.path.join(SavePath,f)
            
            if os.path.exists(pathTarget):
                pathTarget = pathTarget[:-4] + "_copy" + pathTarget[-4:]
                
            shutil.copyfile(pathOriginal, pathTarget)

### remove folders into Photos if you want
for d in os.listdir(SavePath):
    if os.path.isdir(os.path.join(SavePath,d)):
        shutil.rmtree(os.path.join(SavePath,d))&lt;/LI-CODE&gt;&lt;P&gt;Hope this help you,&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Fred&lt;/P&gt;</description>
      <pubDate>Fri, 28 May 2021 07:04:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/using-attachmentmanager-to-download-attachments/m-p/1062764#M6142</guid>
      <dc:creator>FrédéricPRALLY</dc:creator>
      <dc:date>2021-05-28T07:04:54Z</dc:date>
    </item>
    <item>
      <title>Re: Using AttachmentManager to Download Attachments Saves in Separate Folders</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/using-attachmentmanager-to-download-attachments/m-p/1062851#M6144</link>
      <description>&lt;P&gt;Thank you.&amp;nbsp; I was able to go through &lt;A href="https://community.esri.com/t5/arcgis-online-documents/downloading-feature-layer-attachments-via-the-arcgis-api-for/ta-p/916538" target="_self"&gt;this one&lt;/A&gt; and make one that worked out for me.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 May 2021 14:09:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/using-attachmentmanager-to-download-attachments/m-p/1062851#M6144</guid>
      <dc:creator>cschooley</dc:creator>
      <dc:date>2021-05-28T14:09:21Z</dc:date>
    </item>
    <item>
      <title>Re: Using AttachmentManager to Download Attachments Saves in Separate Folders</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/using-attachmentmanager-to-download-attachments/m-p/1075762#M6323</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/482199"&gt;@cschooley&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have a look at this tool I created - might be helpful:&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/t5/arcgis-pro-blog/export-attachments-toolbox/ba-p/1022018" target="_blank"&gt;https://community.esri.com/t5/arcgis-pro-blog/export-attachments-toolbox/ba-p/1022018&lt;/A&gt;&lt;/P&gt;&lt;H6&gt;If this answer solved your question or if you found it helpful please mark it accordingly to help others who have the same question.&lt;/H6&gt;</description>
      <pubDate>Tue, 06 Jul 2021 06:12:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/using-attachmentmanager-to-download-attachments/m-p/1075762#M6323</guid>
      <dc:creator>UriGilad_EsriAu</dc:creator>
      <dc:date>2021-07-06T06:12:02Z</dc:date>
    </item>
  </channel>
</rss>

