Consider the following Python file structure
Root
|---- SubFolder
| |---- generator.py
| |---- config.json
|
|---- main.py
main.py:
import os
def main():
from SubFolder.generator import start
start(__file__)
if __name__ == '__main__':
main()
generator.py
import os
def start(caller: str):
file_path_s = "./config.json"
file_path_l = "./SubFolder/config.json"
exist_s = os.path.exists(file_path_s)
exist_l = os.path.exists(file_path_l)
print(f"Caller: {caller}")
print(f"cwd:{os.getcwd()}")
print(f"exist {file_path_s}: {exist_s}")
print(f"exist {file_path_l}: {exist_l}")
if __name__ == '__main__':
start(__file__)
The probplem:
generator.start()
can be called from main.py
as well as from generator.py
itself (executed as main)
accordigly the working directory is different
when start()
wants to read the json, the path to to the json is different each case.
when called from main it should use ./SubFolder/config.json
when called from generator it should use ./config.json
Is there a way to figure out the path to use without the start()
will do something like the following:
def _get_config_path():
work_dir = os.getcwd()
sub_folder = "SubFolder"
index = work_dir.rfind(sub_folder)
expected = len(work_dir) - len(sub_folder)
if index == expected:
return "./config.json"
else:
return "./SubFolder/config.json"
how this can be achived?
In generator.py just use a relative path to your config
from pathlib import Path
def start(caller: str):
config_path = Path(__file__).parent / "config.json"
if config_path.exists():
print(f"config exists: caller {caller} does not matter")
if __name__ == '__main__':
start(__file__)
Then it doesn't matter who calls it.