Exporting arcGIS attachments using arcpy

970
5
Jump to solution
01-24-2023 11:04 AM
Chase_Mohrman
New Contributor II

I'm trying to export all attachments from an attachment table in a gdb to a folder. I'm using a script I found here. I keep getting the error that item[0] is None and so has no attribute 'tobytes'. This table is a standard ATTACH table with the default fields. I appreciate any suggestions!

inTable = [path to ATTACH table] fileLocation = [Export folder]

for fc in list: print(fc)

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

Traceback (most recent call last): File "C:\Users\gscmm\PycharmProjects\pythonProject2\AttachmentDownloader.py", line 33, in open(fileLocation + os.sep + filename, 'wb').write(attachment.tobytes()) AttributeError: 'NoneType' object has no attribute 'tobytes'

1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

To post code:

JohannesLindner_0-1674589814049.png

JohannesLindner_1-1674589827880.png

 

 

You probably have empty attachments. Try replacing the SearchCursor line with this:

with da.SearchCursor(inTable, ['DATA', 'ATT_NAME', 'ATTACHMENTID'], 'DATA IS NOT NULL') as cursor:

 


Have a great day!
Johannes

View solution in original post

5 Replies
JohannesLindner
MVP Frequent Contributor

To post code:

JohannesLindner_0-1674589814049.png

JohannesLindner_1-1674589827880.png

 

 

You probably have empty attachments. Try replacing the SearchCursor line with this:

with da.SearchCursor(inTable, ['DATA', 'ATT_NAME', 'ATTACHMENTID'], 'DATA IS NOT NULL') as cursor:

 


Have a great day!
Johannes
Chase_Mohrman
New Contributor II

Thank you. For some reason, the very first attachment was empty. All I had to do was skip the first one and the rest import properly. Thank you!

0 Kudos
Brownschuh
Occasional Contributor II

Glad I found this post; the code you posted worked.  Looks like I have cleanup to do as well ... time to identify points without attachments.

0 Kudos
ChrisRingo
Occasional Contributor

You might get more responses if you format your code - it's very difficult to see what's going on as you've posted it. Here's some community guidelines for inserting code into a post: https://community.esri.com/t5/community-help-documents/how-to-insert-code-in-your-post/ta-p/914552

Also might want to include other details like how are you running this code (python window, PyCharm, Toolbox, etc). Also, the output of your print statements would be useful. If this is the entire code it seems you haven't imported os. Also, did you append "_ATTACH" to the end of your table name? It's hard to help unless you supply all the details...

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

It's hard to tell without an example, but I think you might be opening the attachment without a file extension.

0 Kudos