Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pygame load
#1
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
Reply
#2
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)
Recommended Tutorials:
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pycharm dosnt load pygame Matres 1 1,968 Mar-16-2020, 02:15 AM
Last Post: Larz60+
  [pyGame] Load Image, Problem Found ! JamieVanCadsand 2 8,759 Sep-29-2017, 06:26 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020