
path sanitizer 1
By:
waterapple on
Oct 4th, 2012 | syntax:
Python | size: 1.30 KB | hits: 35 | expires: Never
#-------------------------------------------------------------------------------
# Name: module1
# Purpose:
#
# Author: new
#
# Created: 05/10/2012
# Copyright: (c) new 2012
# Licence: <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import os,re
def sanitizepath(pathin):
print pathin
assert(type(pathin)==type(""))
segments = []
workingpath = pathin# make a copy for easier debugging
#split the path into segments
while True:
workingpath, segment = os.path.split(workingpath)
print segment
segments.append(segment)
if len(workingpath) <= 0:
break
segments.reverse()
print segments
#sanitize segments
disallowedchars =r'<>:"/\|?*'
precessedsegments = []
for segment in segments:
print segment
s1 =segment.strip()
s2 = re.sub('[<>:"/\|?*]+', '-',segment)
s3 =segment.strip()
s4 =segment.strip(".")
print s4
precessedsegments.append(s4)
#join segments
pathout = os.path.join(precessedsegments)
print pathout
def main():
sanitizepath("download/tangent/Tangent/Text/Archives.../Zach")
if __name__ == '__main__':
main()