How do I access a local folder through its filename?

前端 未结 1 598
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-14 15:17

I have a folder that is my animation/game, and in the folder is the .pyw file and a .wav music file. I have

import wave
wave.open()

and I d

相关标签:
1条回答
  • 2021-01-14 16:17

    A common way this is done is by using the path of the current module, which is automatically available in the predefined__file__variable, to determine the path to the file in the subdirectory:

    import os
    import wave
    
    mydir = os.path.dirname(__file__)
    subdir = 'sounds'
    wavefilepath = os.path.join(mydir, subdir, 'Music.wav')
    wave.open(wavefilepath)
    
    0 讨论(0)
提交回复
热议问题