Inconsistency in python system attribute "__file__" on different linux distros
I am dealing with weird problem. Consider this simple code:
print(__file__) print("***" + os.path.dirname(__file__) + "###")
now when I run this code on my laptop, where I have Fedora 23 OS, the output is:
running the same code on Ubuntu 15.10:
can anybody explain me why? Am I missing something?
You probably don't want to purely rely on the __file__ attribute, but you ensure you have a full path abspath:
__file__ is defined by the argument given to Python that tells it what your file is. For example, let's say you are in a folder called Here. You could say python ../Here/here.py, and __file__ would be ../Here/here.py. If you ran the same file with just python here.py, __file__ is here.py. When you run the Python file in the same directory without using either an absolute path or a weird relative path, the file will not have any directories mentioned. That means that os.dirname(__file__) will be blank. If you want, you can get the absolute path with os.path.abspath().