import os import sys import re dir sys argv RE_UPPER re compile A-Z0-9

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import os
import sys
import re
dir = sys.argv[1]
RE_UPPER = re.compile(r'^[- A-Z0-9]+$')
APPEND = '<'
PREPEND = '>'
for fname in os.listdir(dir):
path = os.path.join(dir, fname)
if os.path.isfile(path) and path.endswith('.txt'):
print 'Processing %s' % path
lines = []
for line in file(path):
line = line.strip()
if RE_UPPER.match(line):
lines.append(PREPEND + line + APPEND)
else:
lines.append(line)
file(path, 'w').write('\n'.join(lines))