Actually I found the solution of my main problem "Get the full path to the directory a Python file is contained in" from the previous answer : Find current directory and file's directory.
And the code below from the answer works well if I run my entire script, in other words, hotkey F5.
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
However, if I just select the two lines of the above code and run it, in other words, hotkey F9. Then I will receive the error below:
NameError: name '__file__' is not defined
So if anyone happens to know why the error occurs, please give a brief explanation.
Thanks a lot!
By the way, i used Spyder (Python 2.7).