[Tutor] sys.getfilesystemencoding()
Albert-Jan Roskam
fomcl at yahoo.com
Tue Dec 18 15:38:40 CET 2012
----- Original Message -----
> From: eryksun <eryksun at gmail.com>
> To: Albert-Jan Roskam <fomcl at yahoo.com>
> Cc: Python Mailing List <tutor at python.org>
> Sent: Tuesday, December 18, 2012 2:52 PM
> Subject: Re: [Tutor] sys.getfilesystemencoding()
>
> 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
Hi Peter, Eryksun,
Thank you for your replies. My code example was simplified, maybe too simplified. The parameter <fn> actually
is a parameter of a C function (c_char_p). So I have to encode it as a byte string.
Good to know though that when writing files 'the regular way' things are a bit easier. Judging from the squares/tofu, the symbols can not be represented on my system. I installed a telugu.ttf to see it that would solve it, but unless I need to reboot first, it doesn't.
Why I want to do this, you ask? Just 'because'. ;-) It's fun to try and make one's program crash and look for the solution.
Thanks again guys, I appreciate it.
Albert-Jan
More information about the Tutor
mailing list