code: def encode_filename(filename, from_encoding='utf-8', to_encoding=None):
      
      import sys
      to_encoding = to_encoding or sys.getfilesystemencoding()
      from_encoding = from_encoding or sys.getfilesystemencoding()
      if not isinstance(filename, unicode):
          try:
              f = unicode(filename, from_encoding)
          except UnicodeDecodeError:
              try:
                  f = unicode(filename, 'utf-8')
              except UnicodeDecodeError:
                  raise Exception, "Unknown encoding of the filename %s" % filename
          filename = f
      if to_encoding:
          return filename.encode(to_encoding)
      else:
          return filename
docs: >>> print encode_filename('\xe4\xb8\xad\xe5\x9b\xbd.doc')
      \xd6\xd0\xb9\xfa.doc
      >>> f = unicode('\xe4\xb8\xad\xe5\x9b\xbd.doc', 'utf-8')
      >>> print encode_filename(f)
      \xd6\xd0\xb9\xfa.doc
      >>> print encode_filename(f.encode('gbk'), 'gbk')
      \xd6\xd0\xb9\xfa.doc
      >>> print encode_filename(f, 'gbk', 'utf-8')
      \xe4\xb8\xad\xe5\x9b\xbd.doc
      >>> print encode_filename('\xe4\xb8\xad\xe5\x9b\xbd.doc', 'utf-8', 'gbk')
      \xd6\xd0\xb9\xfa.doc