def sab_sanitize_foldername(name):
 CH_ILLEGAL = '\\/<>?*|"'
 CH_LEGAL = '++{}!@#`'
 FL_ILLEGAL = (CH_ILLEGAL + ':\x92"')
 FL_LEGAL = (CH_LEGAL + "-''")
 uFL_ILLEGAL = FL_ILLEGAL.decode('latin-1')
 uFL_LEGAL = FL_LEGAL.decode('latin-1')
 if (not name):
  return name
 if isinstance(name, unicode):
  illegal = uFL_ILLEGAL
  legal = uFL_LEGAL
 else:
  illegal = FL_ILLEGAL
  legal = FL_LEGAL
 lst = []
 for ch in name.strip():
  if (ch in illegal):
   ch = legal[illegal.find(ch)]
   lst.append(ch)
  else:
   lst.append(ch)
 name = ''.join(lst)
 name = name.strip('. ')
 if (not name):
  name = 'unknown'
 return name

def sab_sanitize_foldername ( name ) : CH_ILLEGAL = '\\/<>?*|"' CH_LEGAL = '++{}!@#`' FL_ILLEGAL = ( CH_ILLEGAL + ':\x92"' ) FL_LEGAL = ( CH_LEGAL + "-''" ) uFL_ILLEGAL = FL_ILLEGAL . decode ( 'latin-1' ) uFL_LEGAL = FL_LEGAL . decode ( 'latin-1' ) if ( not name ) : return name if isinstance ( name , unicode ) : illegal = uFL_ILLEGAL legal = uFL_LEGAL else : illegal = FL_ILLEGAL legal = FL_LEGAL lst = [ ] for ch in name . strip ( ) : if ( ch in illegal ) : ch = legal [ illegal . find ( ch ) ] lst . append ( ch ) else : lst . append ( ch ) name = '' . join ( lst ) name = name . strip ( '. ' ) if ( not name ) : name = 'unknown' return name
return foldername with dodgy chars converted to safe ones remove any leading and trailing dot and space characters .