how to get a specific file(sound file) when the file name is called by a variable in using Pyglet

201 Views Asked by At

When using module pyglet, giving the name of mp3 explicitly like sound="10.mp3", it works. When using module playsound and giving filenames as variable, playsound(str(play_num) + '.mp3'), it does work.

playsound(str(play_num)+'.mp3')

But when applying variable name of file to pyglet just like below, sound = "str(play_num)+'.mp3'", it does not work.

sound = "str(play_num)+'.mp3'"

it shows FileNotFoundError. How can I solve this problem??? In addition how to get the files from other directory and some other folder? When getting a sound file in other directory/folder using variable ?

player = pyglet.media.Player()
sound = "str(play_num)+'.mp3'"
src = pyglet.media.load(sound)
player.queue(src)
1

There are 1 best solutions below

0
On

Use F-String:

sound = f"{play_num}.mp3"