1

I have a folder with 3 subfolder, each having a different type of data. Folder_A and Folder_B have standard shapefiles (about 3 shapefiles in each folder). Folder_C has .tif grid files (about 100). I am trying to write a Python script that will create the metadata files (xml) for each of these files in the subfolder. I was looking at using arcpy.SynchronizeMetadata_conversion, but something seems awry. It looks like it is primarily designed for features in a .gdb. Also, it doesn't have an output file. Or you cannot name one anyways. Is there another way to do this? :

import arcpy, lxml, copy, sys, os
from lxml import etree
from arcpy import env

mydir = "<file path to Spatial_Files folder>"

def list_files(dir):
    r = []
    for root, dirs, files in os.walk(dir):
        for name in files:
            print(name)
            r.append(os.path.join(root, name))
        return r

def list_folders(dir):
    d = []
    for root, dirs, files in os.walk(dir):
        for dir in dirs:
            print(dir)
            d.append(os.path.join(root, dir))
        return d

subdirs = list_folders(mydir)
for subdir in subdirs:
    files = list_files(subdir)
    for file in files:
        arcpy.env.workspace = subdir
        arcpy.SynchronizeMetadata_conversion(file, "CREATED")
3

I think you are barking up the wrong tree here. ArcPy has little or no support for creating metadata in a scripting environment.

But help is to the rescue! A group in the USA have developed a ArcPy Metadata Editor (arcpy_metadata) module at github that helps you create and/or update metadata.

They have abstracted away the need for delving into pure XML and you treat the metadata as properties of an object.

  • This looks like a great solution. I have it installed and I was able to import it into a script. Unfortunately, when I try to enter a shapefile in as a parameter (so I can look at the metadata already showing for that item), it keeps throwing a TypeError: unhashable type: 'dict' Any ideas on how to resolve this? – gwydion93 Aug 30 '18 at 17:55
  • I was actually able to get this working using the arcpy_metadata mod/library. It's super simple, but has some limitations. 1) It only runs with Python 2.7, 2) It's 32-bit, 3) It can't add spatial reference or extent elements (also some of the contact data is a little wonky). Other than that, I made some loops to iterate through the target folder, some conditionals to handle for different file types (only works on features, .lyrs, .shp, and raster datasets). Handling some of these file types is a little difficult but for limited use, it works pretty good. – gwydion93 Sep 5 '18 at 14:53
  • I wonder if you should contact the author and suggest the improvements, especially not being able to add specific elements? Good point about 2.7. – Hornbydd Sep 5 '18 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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