f = file(file_name,'w')
return f
+def getfilesystemencoding():
+ try:
+ # sys.getfilesystemencoding() was introduced in Python v2.3
+ return sys.getfilesystemencoding()
+ except AttributeError:
+ return 'ascii'
def os_encode(s):
- return s.encode(sys.getfilesystemencoding())
+ # Encoding can be None, meaning the default system encoding should
+ # be used.
+ enc = getfilesystemencoding()
+ if enc:
+ return s.encode(enc)
+ else:
+ return s
def os_decode(s):
- if type(s) == unicode:
+ # Encoding can be None, meaning the default system encoding should
+ # be used.
+ enc = getfilesystemencoding()
+ if type(s) == unicode or not enc:
return s
else:
- return unicode(s,sys.getfilesystemencoding())
+ return unicode(s,enc)
def write(file_path,content):
f = create_file(file_path)