0

This question exists on this website but it is flawed as it returns a <type 'str'> (both when using __file__ and when using the module inspect) and not a <type 'unicode'> which I need as my path contains non-ascii characters (Hebrew characters, to be specific).

How can I get the path and filename of current running script in <type 'unicode'> in the file system encoding? Is there any Unicode-friendly function built-in for doing so or any solution?

Just calling __file__.encode(sys.getfilesystemencoding()) fails with ascii codec can't decode byte ....

Community
  • 1
  • 1
Jonathan
  • 123
  • 2
  • 7

2 Answers2

3

You need .decode(), not .encode(). To go from unicode to str, one encodes. To go the other direction, one decodes.

Try:

import sys
filename = __file__.decode(sys.getfilesystemencoding())
print type(filename), filename

Result:

$ python 'קוֹבֶץ.py'
<type 'unicode'> קוֹבֶץ.py
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • It all makes sense now! Thank you so much – Jonathan May 25 '16 at 17:04
  • @Jonathan: note: the error in your question is due to `.encode()` method tries to *decode* your bytestring using `sys.getdefaultencoding()` (that should be ascii on Python 2) before encoding it using `sys.getfilesystemencoding()` that works for representable characters (on Linux, your local settings should be correct). – jfs May 26 '16 at 13:30
1

I guess you want to use decode instead of encode:

>>> type('string'.encode('utf-8'))
<type 'str'>
>>> type('string'.decode('utf-8'))
<type 'unicode'>

Genrally, encode returns a bytestring, decode a unicode object.

user2390182
  • 72,016
  • 6
  • 67
  • 89