How to make an "always relative to current module" file path? [duplicate]
Asked Answered
S

2

102

Let's say you have a module which contains

myfile = open('test.txt', 'r')

And the 'test.txt' file is in the same folder. If you'll run the module, the file will be opened successfully.

Now, let's say you import that module from another one which is in another folder. The file won't be searched in the same folder as the module where that code is.

So how to make the module search files with relative paths in the same folder first?

There are various solutions by using "__file__" or "os.getcwd()", but I'm hoping there's a cleaner way, like same special character in the string you pass to open() or file().

Stanwinn answered 16/4, 2012 at 12:37 Comment(3)
What's the problem with os.getcwd()?Eames
os.getcwd() returns the path of the first module, which imported that module, or imported another one which imported it. The first module might be in many directories up or down. So it doesn't seem very elegant to type os.getcwd()+'something/somethingelse/yetanother/finallyhere'Stanwinn
No; os.getcwd() returns the current working directory of the process, which does not necessarily have anything at all to do with any module at all.Iniquity
F
127

The solution is to use __file__ and it's pretty clean:

import os

TEST_FILENAME = os.path.join(os.path.dirname(__file__), 'test.txt')
Fridell answered 16/4, 2012 at 13:4 Comment(4)
import os.path is pointless, when you do import os, the module imports os.path for you (os is special that way). One could use from os import path but it's not common in case of the os module. It's very probable he already has import os in his module or will need one soon. Therefore, import os is almost always the best choice.Fridell
I guess this is as clean as you can get. Anything more would be asking to change how the official Python interpreter works.Stanwinn
The fact that os already has imported os.path is an implementation detail. While the convention has been established by now and is unlikely to change, you should not rely implementation details. When using os.path, explicitly importing os.path is never pointless.Dialytic
Do add os.path.realpath() to get an absolute path first.Dialytic
H
54

For normal modules loaded from .py files, the __file__ should be present and usable. To join the information from __file__ onto your relative path, there's a newer option than os.path interfaces available since 2014:

from pathlib import Path

here = Path(__file__).parent
fname = here / "test.txt"
with fname.open() as f:
    ...

pathlib was added to Python in 3.4 - see PEP428. For users still on Python 2.7 wanting to use the same APIs, a backport is available.

Note that when you're working with a Python package, there are better approaches available for reading resources - you could consider moving to importlib-resources. This requires Python 3.7+, for older versions you can use pkgutil. One advantage of packaging the resources correctly, rather than joining data files relative to the source tree, is that the code will still work in cases where it's not extracted on a filesystem (e.g. a package in a zipfile). See How to read a (static) file from inside a Python package? for more details about reading/writing data files in a package.

Hetzel answered 10/9, 2018 at 23:7 Comment(2)
Path (from pathlib) provides so many convenient cross-platform features that this is going to be the tidiest approach overall, compared to os.Imprudent
Since it is using pathlib this is my preferred solution.Wozniak

© 2022 - 2024 — McMap. All rights reserved.