Friday, October 02, 2009
Stupid python tricks - list of paths from a file
Take a long, repetitive list of Windows filenames, like C:\TEMP\blah.txt, and return the unique pathnames from this list:
#!/usr/bin/python
import re
results={}
f = open('filelist.txt','r')
for line in f:
m=re.search(r"(.:\\\S+)",line)
if m != None:
path=m.group(0).rpartition('\\')
results[path[0]]=True
pathlist=results.keys()
pathlist.sort()
for path in pathlist:
print path