pythonpython-3.xmodulemp3python-playsound

Got the error:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe3 in position 1: invalid continuation byte, playsound


I was making a program that plays a song from playsound but I got the error:"UnicodeDecodeError:'t decode byte 0xe3 at position 1: invalid continuation byte" What to do to play the song.

This was my code:

from playsound import playsound


playsound('C://Users//hp//Desktop//francisco//non-school work//musica.mp3')

This was the error:

Traceback (most recent call last):
  File "c:/Users/hp/Desktop/francisco/trabalhos que não são da escola/projeto1/ex021.py", line 3, in <module>
    playsound('C:/Users/hp/Desktop/francisco/trabalhos que não são da escola/musica.mp3')
  File "C:\Users\hp\AppData\Local\Programs\Python\Python38\lib\site-packages\playsound.py", line 35, in _playsoundWin
    winCommand('open "' + sound + '" alias', alias)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python38\lib\site-packages\playsound.py", line 30, in winCommand
    '\n    ' + errorBuffer.value.decode())
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe3 in position 1: invalid continuation byte

I tried to remove double slashes and shows another error:

from playsound import playsound

playsound('C:\Users\hp\Desktop\francisco\trabalhos que não são da escola\musica.mp3')
input()

The error:

 File "c:/Users/hp/Desktop/francisco/trabalhos que não são da escola/projeto1/ex021.py", line 3
    playsound('C:\Users\hp\Desktop\francisco\trabalhos que não são da escola\musica.mp3')
              ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

I already solved the problem just doing this:

from playsound import playsound

playsound('musica.mp3')
input()

What was the problem???


Solution

  • It seems that the file was written in a different encoding than what the playsound module is expecting.

    If you have created the mp3 file yourself, maybe you have not chosen the right encoding.

    Otherwise, it might be a bug of the playsound module.

    Update: The error message actually refers to the file path you are providing, not the file itself. As you are working on Windows and paths are build with backslashes, which are a special character in Python strings, you need to use either double backslashes instead of a single backslash as in

    'C:\\Users\\hp\\Desktop\\francisco\\trabalhos que não são da escola\\musica.mp3'
    

    or you provide the string as a raw string by putting an "r" before the first quotation mark, like this:

    r'C:\Users\hp\Desktop\francisco\trabalhos que não são da escola\musica.mp3'
    

    Update 2: Combining f-string and r-string

    from pathlib import Path
    
    Path(rf"{r'C:\Users'}").exists()
    File "<ipython-input-45-dc9a3746d390>", line 1
    Path(rf"{r'C:\Users'}").exists()
                 ^
    SyntaxError: f-string expression part cannot include a backslash
    
    
    Path(rf"{'C:'}\{'Users'}").exists()
    Out[46]: True
    Path(fr"{'C:'}\{'Users'}").exists()
    Out[47]: True