def sanitize_file_name(name, substitute='_', as_unicode=False):
 if isinstance(name, unicode):
  name = name.encode(filesystem_encoding, 'ignore')
 _filename_sanitize = re.compile('[\\xae\\0\\\\|\\?\\*<":>\\+/]')
 one = _filename_sanitize.sub(substitute, name)
 one = re.sub('\\s', ' ', one).strip()
 (bname, ext) = os.path.splitext(one)
 one = re.sub('^\\.+$', '_', bname)
 if as_unicode:
  one = one.decode(filesystem_encoding)
 one = one.replace('..', substitute)
 one += ext
 if (one and (one[(-1)] in ('.', ' '))):
  one = (one[:(-1)] + '_')
 if one.startswith('.'):
  one = ('_' + one[1:])
 return one

def sanitize_file_name ( name , substitute = '_' , as_unicode = False ) : if isinstance ( name , unicode ) : name = name . encode ( filesystem_encoding , 'ignore' ) _filename_sanitize = re . compile ( '[\\xae\\0\\\\|\\?\\*<":>\\+/]' ) one = _filename_sanitize . sub ( substitute , name ) one = re . sub ( '\\s' , ' ' , one ) . strip ( ) ( bname , ext ) = os . path . splitext ( one ) one = re . sub ( '^\\.+$' , '_' , bname ) if as_unicode : one = one . decode ( filesystem_encoding ) one = one . replace ( '..' , substitute ) one += ext if ( one and ( one [ ( - 1 ) ] in ( '.' , ' ' ) ) ) : one = ( one [ : ( - 1 ) ] + '_' ) if one . startswith ( '.' ) : one = ( '_' + one [ 1 : ] ) return one
sanitize the filename name .