1

For example:

in the file main/main.py

import core.fun as f
f.script()

and in the file main/core/fun.py:

def script():
    f = open('test.txt','r')

where a file test.txt is present in the folder main/core/.

If I run main/main.py it will not work because python will search for the file 'test.txt' in the main folder and not in the main/core folder.

And if I call within the function script()

import os
os.getcwd()

It will again return the directory main

Therefore I would like to know how can I find the directory of the function within the function.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    Does this answer your question? https://stackoverflow.com/questions/9271464/what-does-the-file-variable-mean-do – match Dec 25 '21 at 20:54
  • 1
    Take a look at this question https://stackoverflow.com/questions/50673566/how-to-get-the-path-of-a-function-in-python – Lorenzo Battistela Dec 25 '21 at 20:55

0 Answers0