Python Forum

Full Version: Pygame load
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello
I'm relatively new to Python. Whilst writing an mp3 player I encounter an error.

I have in a previous step created a simple textfile with paths to mp3 files. After reading the file I use a for loop
pygame.mixer.init()
for i, line in enumerate(f):
    if i == 0:
        print(line)
        path, filename = os.path.split(line)
So far nothing strange
For testing purposes I also write variable line (full path to mp3 file) as well as variable path and filename after the split.
And change the working directory
        print('Path: ' + path)
        print('Filename: ' + filename)
        os.chdir(path)
No to call the player (pygame) I use filename in the load() function and then play
        pygame.mixer.music.load(filename)
        pygame.mixer.music.play()
Getting the error pygame.error: "Couldn't open '01-som_vatten.mp3"
If I instead directly write
pygame.mixer.music.load('01-som_vatten.mp3')
It works fine.

Output of the prints are:
Path: C:\temp\ett\tva
Filename: 01-som_vatten.mp3

I cannot work out what is the difference between writing the mp3 filename directly and using a variable. Could it be the wrong variable typ?

Regards
Johan
As long as your variable is a string then it should be fine.
Are you joining the path and filename beforehand that we cannot see? What is filename before this line of code occurs?
Quote:
pygame.mixer.music.load(filename)
Thanks for responding metulburr.

It turns out to be something else.

I started wondering about the error message:
pygame.error: Couldn't open 'C:/temp/ett/tva/01-som_vatten.mp3
'
Why is the ' at a separate line? It turns out the the CR LF from the files is there thus the file path ends a whitespace.
After removeing it with .strip() it works fine.

Regards
Johan