Skip to content Skip to sidebar Skip to footer

Having A Relative Path In A Module Relative To The File Calling The Module

Let's say I have the current file structure : app\modules\module.py app\modules\somefile.txt app\scripts\script.py app\main.py modules\script.py : import sys sys.path.append('..\

Solution 1:

I'm not sure what all you're doing, but since somefile.txt is in the same folder as module.py, you could make the path to it relative to the module by using its predefined __file__ attribute:

import os
fileToRead = os.path.join(os.path.dirname(__file__), "somefile.txt")

Post a Comment for "Having A Relative Path In A Module Relative To The File Calling The Module"