[Tutor] myown.getfilesystemencoding()

Albert-Jan Roskam fomcl at yahoo.com
Fri Aug 30 17:04:14 CEST 2013


In Windows, sys.getfilesystemencoding() returns 'mbcs' (multibyte code system), which doesn't say very much imho.
So I wrote the function below, which returns the codepage as reported by the windows chcp command. I noticed that 
the function returns 850 (codepage 850) when I run it via the command prompt, but 1252 (cp1252) when I run it in my IDE (Spyder).
Any idea why? Is it a good idea anyway to make this function (no, probably, because Python devs are smart people ;-)
 
#!python.exe
#Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
 
import subprocess, re, sys
 
def getfilesystemencoding():
    if sys.platform.startswith("win"):
        proc = subprocess.Popen("chcp", shell=True, stdout=subprocess.PIPE)
        m = re.search(": (?P<codepage>\d+)", proc.communicate()[0])
        if m:
            return m.group("codepage")
        return sys.getfilesystemencoding()
    return sys.getfilesystemencoding()

print getfilesystemencoding()

Regards,
Albert-Jan


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a 
fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     


More information about the Tutor mailing list