open files from another directory from python in linux

I have a file in Linux called testing.txt.

Below is my project structure in Linux

├── base_dir
│   └── helper
│       └──
│       └──
    └── jobs


├── share_dir
│   └── testing.txt     

├── scripts
│   └──       

Run mechanism

1) Using I will run the `jobs/` script. 
2) In jobs/ I have from helper.file_helper import *
3) In helper/ I will read the testing.txt file

helper/ contents


with open("main_project/share_dir/testing.txt") as f:
    data = {}
    for line in f:
        key,value = line.strip().split('#')
        data[key] = value

When I call the like below I do not have any issues

sh /home/$USER/main_project/scripts/ test_job

When I call the like below

# change directory to main_project(this is not in
cd main_project

# run the scripts
sh scripts/ test_job

Then I am getting NO file or directory main_project/share_dir/testing.txt error

I want to read the testing.txt file without any issues when calling the from any directory

How can I resolve this issue


This starts from the file’s own location (inside helper) and constructs the actual location of main_project:

mainproj = os.path.realpath( os.path.dirname(__file__)+"/../.." )
with open( mainproj + "/share_dir/testing.text") as f: