0

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?

Fred James
  • 53
  • 1
  • 7

2 Answers2

0

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

  • After importing the directory using import, how do you reference it when getting the file? For instance, I need to get an audio file called music.wav from resources. How would I go about getting it? – Fred James Sep 26 '18 at 17:22
  • Is there a specific library you are using in your program? For instance, I generally make games using `pygame` and I set the sound doing `sound = pygame.mixer.Sound("file")` I did a little looking into how to do it outside of pygame. I found this document and hope it will help you out. https://docs.python.org/2/library/wave.html – Nolan Melander Sep 26 '18 at 18:47
  • I am using pyaudio. I am actually using wave to take in the file. I was just wondering if you could specify the package and file name or do some kind of relative path based on the resource root instead of using the absolute path or doing something like ../audio/music.wav. – Fred James Sep 26 '18 at 21:17
  • With my programs, I tend to use a relative path for instance in one of my games my files are set up as `game/Interface/file.py` and `game/Resources/Sounds` and I just do `self.sound = pygame.mixer.Sound("..\Resources\Sounds\Letters\Y.ogg")` I haven't used pyaudio but I believe the same concept should apply. The documentation for pyaudio should have documentation on this. – Nolan Melander Sep 26 '18 at 21:45
  • I can use a relative directory as well in my case, but the point was to try to have a directory-independent way to get the file. Basically, I don't want it to be an absolute path so it will work on other people's computers no matter where the project is put. But I don't want it to be a simple relative path like ../audio/music.wav because, if the file is run from a different path in the directory, it will fail. For instance, if the file is executed from the project directory (e.g. python3 package/runner.py) instead of running it from the package directory (python3 runner.py), it would fail. – Fred James Sep 27 '18 at 02:28
  • Basically, if I could have something like the answer here that gets the filepath instead of the file contents given the package and resource name, it would work perfectly: https://stackoverflow.com/a/8428564/10409968 – Fred James Sep 27 '18 at 02:33
  • Unfortunately, I haven't done anything on that scale before. Hopefully, someone else will have a better answer for you. – Nolan Melander Sep 28 '18 at 21:14
0

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'
Fred James
  • 53
  • 1
  • 7