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'
Solved! Go to Solution.
To post code:
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:
To post code:
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:
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!
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.
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...
It's hard to tell without an example, but I think you might be opening the attachment without a file extension.