lets assume i have a Folder "myProject" with a script "mySkript.py" and a config file "myConfig.py".
When calling the script from within "myProject" i would do something like this:
with open("myConfig") as configurationRawData:
# do something
Now lets assume i don't call the script from "myProject" folder but from "User/desktop". In this case, the open command will not actually find "myConfig". I am looking to make my skript use it's own path as root path and inherit this property to every other script it might call during execution. Any ideas?
There is a way to do it :
__file__is a internal python variable that represents the path to the current file (something likeC:\Users\user\documents\scripts\mySkript.pyfor windows per example). It leads to the file itself, it does not depends on working directory.os.path.dirname(__file__)gives you the directory to the current file (C:\Users\user\documents\scripts\for the example above).os.path.join()builds a path like your os likes it, so it will produceC:\Users\user\documents\scripts\myConfig.pyfor the example above.This will work whatever your operating system is (python handles it for you) and as long as your two files are in the same directory.