[Tutor] sys.getfilesystemencoding()

eryksun eryksun at gmail.com
Tue Dec 18 14:52:54 CET 2012


On Tue, Dec 18, 2012 at 8:13 AM, Albert-Jan Roskam <fomcl at yahoo.com> wrote:
>
> # Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19)
>   [MSC v.1500 32 bit (Intel)] on win32
>
> import sys
>
> def _encodeFileName(fn):
>     """Helper function to encode unicode file names into system file names.
>     http://effbot.org/pyref/sys.getfilesystemencoding.htm"""
>     isWindows = sys.platform.startswith("win")
>     isUnicode = isinstance(fn, unicode)
>     if isUnicode:  # and not isWindows
>         encoding = sys.getfilesystemencoding()  # 'mbcs' on Windows, 'utf-8' on Linux
>         encoding = "utf-8" if not encoding else encoding
>         return fn.encode(encoding)
>     return fn


Use unicode filenames. CPython encodes them with the file system
encoding, with an exception to use the internal UTF-16 on Windows. See
fileobject.c (2.6.4 links):

file_init (2170-2185):
http://hg.python.org/cpython/file/8803c3d61da2/Objects/fileobject.c#l2150

open_the_file (269-283):
http://hg.python.org/cpython/file/8803c3d61da2/Objects/fileobject.c#l230


More information about the Tutor mailing list