0

I have a Utils-folder structured like this

Utils/
├─ Data/
│  ├─ file1.txt
├─ utils.py
├─ __init__.py

(Utils is added to PATH) and in utils.py I open and read from ./Data/file1.txt.

The issue is, when I in another script, main.py in another folder, import and run utils.py I get the

FileNotFoundError: [Errno 2] No such file or directory: './Data/file1.txt'

error.

I can fix this by specificing the full-path to the ./Data/ folder in utils.py and then read the file using that (which ofcourse is not that robust), thus I can do it dynamic by getting the path of the utils.py file using the os.path etc.

I assume the issue is that python looks for /Data/file1.txt in the folder of main.py instead of utils.py.

I really doubt that we have to get the full-path to files in the utils.py folder - but is it really so? Cant we make realtive imports from imported packages/scripts?

CutePoison
  • 3,725
  • 4
  • 21
  • 32

0 Answers0