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:

/home/.../ExperimentalSystem/BullshitSetupGenerator.py
***/home/.../ExperimentalSystem###

running the same code on Ubuntu 15.10:

BullshitSetupGenerator_delete_me.py
***###

can anybody explain me why? Am I missing something?

Answers


You probably don't want to purely rely on the __file__ attribute, but you ensure you have a full path abspath:

os.path.dirname(os.path.abspath(__file__))

__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().


Need Your Help

document.ready() function not working

javascript jquery

I am working for selecting the text from a text box to the clipboard with the help of zclip. But the document.ready() is not working. It is not even showing the alert.

Why isn't this print method working?

objective-c printf nslog

It works when I use NSLog, but not printf. I want to use printf because then all the elements in setA will be displayed in one line.