2

I was trying to add some pictures as attachments to my features in a feature class in a .mdb with ArcPy. I'm fairly new to this, so I was just playing with some test data and I modified the demo codes from ArcGIS tutorial like this:

# encoding: utf-8
import arcpy
import _csv
import os
import sys

arcpy.env.workspace = "F:/全国省级、地市级、县市级行政区划shp/全国省级、地市级、县市级行政区划shp/New Personal Geodatabase.mdb"
input = "F:/New Personal Geodatabase.mdb/Province"
inputField = "OBJECTID"
matchTable = r"F:\matchtable.csv"
matchField = "OBJECTID"
pathField = "Picture"
picFolder = r"F:\MGS"

writer = _csv.writer(open(matchTable, "wb"), delimiter=",")
writer.writerow([matchField, pathField])

for file in os.listdir(picFolder):
    if str(file).find(".jpg") > -1:
        writer.writerow([str(file).replace(".jpg", ""), file])

del writer

arcpy.EnableAttachments_management(input)
arcpy.AddAttachments_management(input, inputField, matchTable, matchField, pathField, picFolder)

(Because I'm Chinese, so the file name contained Chinese character. However, in other applications this file name worked with no problem)

Then I got error like this:

Traceback (most recent call last):
  File "F:/���ֿμ��͹�������/ʵϰ/ArcPy Sandbox/AutomateScript/BatchAddAttribute.py", line 29, in <module>
    arcpy.AddAttachments_management(input, inputField, matchTable, matchField, pathField, picFolder)
  File "F:\ArcGIS 10.2.1\Engine10.2\arcpy\arcpy\management.py", line 124, in AddAttachments
    raise e
arcgisscripting.ExecuteError: ERROR 000228: Cannot open the dataset.
Failed to execute (AddAttachments).

ArcGIS version is 10.2

Any idea why?

4
  • Have you tried the same code using only English file and folder names with no spaces to see if it works? Have you tried using the same code using a file rather than personal geodatabase and seeing if it works with exactly the same names?
    – PolyGeo
    Commented Jul 28, 2017 at 5:00
  • You also have a white space in your path. You might change that as well and try it again.
    – Matte
    Commented Jul 28, 2017 at 5:05
  • Try arcpy.Exists(input) and arcpy.Exists(matchTable).. see if Esri thinks they exist as Esri data types. Consider using name, ext = os.path.splitext(file) to split the file name up into "name" and ".ext" then use if ext.upper() == '.JPG': when writing. I have found CSV files to be flaky, it might be less buggy if you use an insert cursor on a geodatabase table then join to that. Surely though you should have the FULL path to the jpg file, use os.path.join(picFolder,file) to get the full path before attachment. With this command there are 3 datasets that could be unable to be opened... Commented Jul 28, 2017 at 5:05
  • I tried to use the attachment tool in ArcGIS and change the table from a .csv to a .xls and it worked, so I think it is because of the .csv file, maybe Esri no longer support them. Thanks for your help!
    – John_Doe
    Commented Jul 28, 2017 at 5:57

1 Answer 1

1

I suspect half your problem is you're trying to match up an objectID to an image name..

writer.writerow([str(file).replace(".jpg", ""), file])

You can't join an OBJECTID (long integer) to a text field so you're going to need a different field. I personally don't use CSV files as I find them to fail at the worst possible time - and you can never trust you've accessed all the records.. perhaps this might work for you:

import arcpy, os, sys

arcpy.env.workspace = "F:/全国省级、地市级、县市级行政区划shp/全国省级、地市级、县市级行政区划shp/New Personal Geodatabase.mdb"
input = "F:/New Personal Geodatabase.mdb/Province"
inputField = "PicName" # field in your feature class that has the name of the picture
matchTable = r"F:\matchtable.dbf"
matchField = "PicName" # field in your new table that has the name of the picture
pathField = "Picture"
picFolder = r"F:\MGS"

arcpy.CreateTable_management("F:\\","MatchTable.dbf")
arcpy.AddField_management(matchTable,matchField,"TEXT",field_length = 255)
arcpy.AddField_management(matchTable,pathField,"TEXT",field_length = 255)

if arcpy.Exists(matchTable):
    try:
        arcpy.Delete_management(matchTable)
    except:
        arcpy.AddError("Unable to purge existing table")
        sys.exit(-1)

with arcpy.da.InsertCursor(matchTable,[matchField,pathField]) as ICur:
    for file in os.listdir(picFolder):
        fName, fExt = os.path.splitext(file)
        if fExt.upper() == '.JPG':
            arcpy.AddMessage(file)
            ICur.insertRow([fName,os.path.join(picFolder,file)])

arcpy.EnableAttachments_management(input)
arcpy.AddAttachments_management(input, inputField, matchTable, matchField, pathField, picFolder)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.