0

In Python can read the filesystem encoding with sys.getfilesystemencoding().

But there seems to be no official way to set the filesystem encoding.

See: How to change file system encoding via python?

I found this dirty hack:

import sys
sys.getfilesystemencoding = lambda: 'UTF-8'

Is there a better solution, if changing environment variable LANG before starting the interpreter is not an option?

Background, why I want this:

This works:

user@host:~$ python src/setfilesystemencoding.py 
LANG: de_DE.UTF-8
sys.getdefaultencoding(): ascii
sys.getfilesystemencoding(): UTF-8

This does not work:

user@host:~$ LANG=C python src/setfilesystemencoding.py 
LANG: C
sys.getdefaultencoding(): ascii
sys.getfilesystemencoding(): ANSI_X3.4-1968
Traceback (most recent call last):
  File "src/setfilesystemencoding.py", line 10, in <module>
    with open('/tmp/german-umlauts-üöä', 'wb') as fd:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 20-22: ordinal not in range(128)

Here is the simple script:

# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, unicode_literals, print_function

import os, sys

print('LANG: {}'.format(os.environ['LANG']))
print('sys.getdefaultencoding(): {}'.format(sys.getdefaultencoding()))
print('sys.getfilesystemencoding(): {}'.format(sys.getfilesystemencoding()))

with open('/tmp/german-umlauts-üöä', 'wb') as fd:
    fd.write('foo')

I hopped that above monkey patching would solve this ... but it doesn't. Sorry, this question does not make sense any more. I close it.

My solution: use LANG=C.UTF-8

guettli
  • 25,042
  • 81
  • 346
  • 663
  • Hmm, the accepted answer of the referenced question explain why you should not even try to set the filesystem encoding. What is your real problem? – Serge Ballesta Dec 07 '17 at 10:39
  • @SergeBallesta I updated the question and explain my background. – guettli Dec 07 '17 at 11:57
  • 1
    You have not explained anything. In particular, I can see neither a real problem nor a real example for changing file system encoding in any of the 2 referenced post, only poor mixes of unicode an byte strings. Once again what is your real problem? – Serge Ballesta Dec 07 '17 at 12:39
  • @SergeBallesta I though changing the filesystemencoding is a solution. But it is not. There seem to be some magic happening during interpreter startup, which can't be changed later. I have not understood up to now why it is not possible to change this value at runtime. I could solve my issue by using "LANG=C.UTF-8" instead of "LANG=C". – guettli Dec 11 '17 at 12:21

0 Answers0