wiki:MOLES/dirupload,

Simple MOLES upload

The following code fragment can be used to upload an entire directory of MOLES documents into eXist.

#
# Simple Routine to load a directory of files into a specific eXist Collection
#
import os
from ndgUtils.eXistInterface import ndg_eXist

def loadCollection(dirName,collectionName,db='chinook.badc.rl.ac.uk'):
    '''walk a directory, load a document '''
    xmldb=ndg_eXist(db=db)
    for root,dirs,files in os.walk(dirName):
        for n in files:
            print n
            f=open(os.path.join(dirName,n),'r')
            doc=f.read()
            path='/'.join([collectionName,n])
            try:
                r=xmldb.storeXML(doc,path)
            except Exception,e:
                print str(e)
if __name__=="__main__":
    import sys
    collection,directory=tuple(sys.argv[1:])
    print 'Storing %s into collection %s'%(directory,collection)
    loadCollection(directory,collection)