Get resource from python resource root

2.9k Views Asked by At

I am using the PyCharm IDE. I marked a folder as a resource root and wanted to get a file from its directory and was wondering the appropriate way to do so.

In Java, you can use getClass().getResource("/resourceName.extension")

Is there some way to get a path from a python in said manner?

2

There are 2 best solutions below

0
On BEST ANSWER

What I want can be accomplished by this answer.

I used the code as follows:

os.path.join(os.path.dirname(__file__), '../audio/music.wav'
7
On

Based off what you have said it sounds like you just need to include the directory of the file with a simple include statement.

for instance, if your files are set up as such:

c:program\main
c:program\resources

then you can just do a simple

import resources

However, you could run into coupling issues if you have any sub-packages. Solving the coupling issue involving resources has been gone over in more detail in another thread I have linked below.

Managing resources in a Python project