Import a Detail file from Directories above

Hi Guys,

Hope everyone of you are having a great day.

My Detail file is not a piece of a script or any code, it’s just some login information which are given as variables.
I have a file under the below structure.
How can I import it and is it possible to keep the path without having a folder structure because I do not want to code that piece of information to every script?

  1. A/B/C ( my Script is inside a Folder in C folder )
  2. My Detail file is in a Folder inside A Folder

Thank you.

What you want in that case is to work with “environmental variables”, the following side goes into some details Using .env Files for Environment Variables in Python Applications - DEV Community

Basically you create a .env file to store the information and load_dotenv will automatically look for that file without you having to specify the path. I put one into my top-level folder for Python projects and it works just fine. I don’t know how good it is in looking for the file so I’d still keep it close.

Seems still I need to specify a Path,

Unless I am in the Parent or Current Directory with the .env file It doesn’t manage to find the file.

To be fair, you should have a central file in a top-level folder. I tested it and I can move several parent-folders up to find the file.

That’s just common practice and propably the way you should just organize it that way.
As for how to traverse folders in general: I think it’s “../” in the path to move to the parent folder.

1 Like

Thee Problemo was Solvdo ! Haha :sweat_smile:

Exactly True to your statement. If we are not working organized we cannot expect answers would work to our issues.

Yeah it worked. The problem was that It doesn’t seem to work when it is inside a Folder at a level several parent-folders up. When I had it outside of a Parent Folder it worked really nice. I don’t have a requirement to have that file inside a Folder. So your solution worked really nicely.

Thank you for chipping in to help me out. As a self taught person I have learnet something new today. May God guide you throughout your life. TC and have a great day.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.