Python __file__ variable returns 'encoding error'

241 Views Asked by At

So my script is in the folder which contains cyrillic symbols in the path and the __file__

variabale returns "encoding error" instead of a real path.

Adding the following line doesnt help

# -*- coding: cp1252 -*-

What should I do?

Version of python 3.3

1

There are 1 best solutions below

0
On

I'm not sure if this answers your question, but...

In python3 the __file__ holds the current running script and path as a str. *nix systems use binary for filenames and have no preference for a particular encoding. When you use __file__ the system will attempt to take the binary string and encode it into a string using the default encoding system. I'm wondering if maybe your filenames are in cp1252 and python is trying to interpret it as utf8.

Python may follow the convention outlined here when encoding __file__: http://docs.python.org/2/library/sys.html#sys.getfilesystemencoding

You didn't state what OS you're using, though...