Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:
>>> import sys
>>> sys.getfilesystemencoding()
'UTF-8'

How do I change that? I know how to change the default system encoding.

>>> reload(sys)
<module 'sys' (built-in)>
>>> sys.setdefaultencoding('ascii')

But there is no sys.setfilesystemencoding.

share|improve this question

3 Answers 3

up vote 3 down vote accepted

The file system encoding is, in many cases, an inherent property of the operating system. It cannot be changed — if, for some reason, you need to create files with names encoded differently than the filesystem encoding implies, don't use Unicode strings for filenames. (Or, if you're using Python 3, use a bytes object instead of a string.)

See the documentation for details. In particular, note that, on Windows systems, the file system is natively Unicode, so no conversion is actually taking place, and, consequently, it's impossible to use an alternative filesystem encoding.

share|improve this answer

From the Python documentation

In Python source code, Unicode literals are written as strings prefixed with the ‘u’ or ‘U’ character:

u = u'abcdefghijk'

Python supports writing Unicode literals in any encoding, but you have to declare the encoding being used. This is done by including a special comment as either the first or second line of the source file:

#!/usr/bin/env python
# -*- coding: latin-1 -*-

u = u'abcdé'
print ord(u[-1])
share|improve this answer
1  
While this is true, it has nothing to do with the file system encoding. – duskwuff Nov 24 '13 at 7:08
    
@duskwuff I am aware. However, as your own answer mentions, the file system encoding cannot be changed. I was merely trying to provide help for the problem I thought that vajrasky actually had - that of using unicode within Python. – Vikram Saran Nov 24 '13 at 7:15

There are two ways to change it:

1) export LANG=en_US.UTF8 before launching python

2) monkeypatching:

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

Both methods let functions like os.stat accept unicode (python2.x) strings. Otherwise those functions raise an exception when they see non-ascii symbols in the filename.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.