Python file name for local folder?
I have a folder which is my animation / game and in the folder is a .pyw file and a .wav music file. I have
import wave
wave.open()
and I don't know what to put in parentheses. I know this must be the filename, but someone might be installing the game in an unknown directory. How to access local folder via filename?
The files are in the same folder and the music is called Music.wav.
+3
source to share
1 answer
A common way to do this is to use the path of the current module, which is automatically available in a predefined variable __file__
, to determine the path to a file in a subdirectory:
import os import wave mydir = os.path.dirname(__file__) subdir = 'sounds' wavefilepath = os.path.join(mydir, subdir, 'Music.wav') wave.open(wavefilepath)
+5
source to share