0

i'm trying to open a file by calling a function in a diffrent folder.

---Folder1
------reader.py
------data.txt
---Folder2
------main.py

(didn't know how to visualize it better)

In reader i try to set the path with os.path.abspath("./data.txt") which works only as long as i run the code within Folder1. When i try to open "data.txt" by calling the function through "main.py" the path it tries to take is ".../Folder2/data.txt".

Now I know that i can just set the path in "main.py" and add it as a parameter, but since I am going to call the "reader.py" from more than one source, this, while possible, isn't optimal. I hope some of you might have a solution, Ignitz

Ignitz
  • 15
  • 3
  • See this question for getting paths relative to file where your code resides: https://stackoverflow.com/questions/9271464/what-does-the-file-variable-mean-do – STerliakov Jun 16 '23 at 00:11
  • Does this answer your question? [what does the \_\_file\_\_ variable mean/do?](https://stackoverflow.com/questions/9271464/what-does-the-file-variable-mean-do) – J_H Jun 16 '23 at 00:19

0 Answers0