forked from krdyke/OGP-metadata-py
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaddUUID.py
More file actions
31 lines (25 loc) · 961 Bytes
/
addUUID.py
File metadata and controls
31 lines (25 loc) · 961 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
__author__ = 'mart3565'
import os
import uuid
import xml.etree.ElementTree as ET
inputDir = r'C:\Users\mart3565\Desktop\mgs-for-testing'
#count the slashes to get starting depth
startDepth = inputDir.count(os.sep)
for root, dirs ,files in os.walk(inputDir):
#if beyond one level, don't do anything with files, and delete dir references
#to avoid recursing further
if root.count(os.sep) - startDepth > 0:
del dirs[:]
else:
for f in files:
if f.endswith('.xml'):
filePath = os.path.join(root,f)
outPath = os.path.join(root, 'updated', f)
uniqueID = str(uuid.uuid4())
tree = ET.parse(filePath)
content = tree.getroot()
titleTag = tree.find('.//title')
titleTag.set('catid', uniqueID)
''' Write tree '''
tree = ET.ElementTree(content)
tree.write(outPath)