I am having issue in fetching the correct path of the file using os.path.abspath(__file__)
.
File: Assessment.py
Actual path: /Users/akshay.dubey/xyz/Assessment.py
python version - 3.7.1
When I am running the script on terminal it shows me a different path, such as /Users/akshay.dubey/xyz/assignment/dataset/Assessment.py
and there is no file Assessment.py
there.
Just to add, I have code to change current working directory to /Users/akshay.dubey/xyz/assignment/dataset
but ideally that should not cause the value for os.path.abspath(__file__)
to change. And when I remove the cwd it shows the correct value.
Code to reproduce:
import os
print(os.path.abspath(os.path.realpath(__file__)))
os.chdir('assignment/dataset1/')
print(os.path.abspath(os.path.realpath(__file__)))
O/P:
>>python untitled1.py
/Users/akshay.dubey/xyz/untitled1.py
/Users/akshay.dubey/xyz/assignment/dataset1/untitled1.py