Get location of the .py source file has some ideas but for example accepted one
# in /a/b/c/d/e/f/file.py
import os
print(os.path.dirname(os.path.abspath(__file__)))
os.chdir("..")
print(os.path.dirname(os.path.abspath(__file__)))
will print
/home/reducing_activity/Desktop/tmp/a/b/c/d/e/f
/home/reducing_activity/Desktop/tmp/a/b/c/d/e
is there a method that will survive os.chdir
?
I am running Ubuntu 20.04 Linux 5.4.0-90-lowlatency #101-Ubuntu SMP PREEMPT Fri Oct 15 20:57:56 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
(from uname -a
, computer name snipped)
With Python 3.8.10
(according to python3 --version
)