Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to get the path to file with python when working directory is different

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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:

  1. generator.start() can be called from main.py as well as from generator.py itself (executed as main)

  2. accordigly the working directory is different

  3. 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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading