14

I already did python pip install playsound and the location of my Python file is also correct please check what is the problem?

It shows cannot specify extra characters after a string enclosed in quotation marks.

My code is:

from playsound import playsound

playsound('C:\\Users\\Lenovo\\OneDrive\\Documents\\Zoom\\1. Chapter 1\\play.mp3\\play.mp3')

Error occured:

enter image description here

Location of sound:

enter image description here

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Arjun Ghimire
  • 301
  • 1
  • 2
  • 8
  • 2
    You Python has `\\play.mp3\\play.mp3`, are you sure you mean to repeat the filename like that? It doesn't match the path you show later on in the question. – larsks Jul 25 '21 at 12:52
  • Thank you I found the solution – Arjun Ghimire Jul 25 '21 at 16:09
  • This bug bug is explained in [this answer](https://stackoverflow.com/a/69237538/774575) and [this one](https://stackoverflow.com/a/72368992/774575). – mins Apr 23 '23 at 10:39

5 Answers5

18

playsound version 1.3.0 has this problem , just downgrade to version 1.2.2 . it will work perfectly fine.

https://pypi.org/project/playsound/1.2.2/

5

Had the same problem, and realized it would work when... retried. This probably led to some incorrect answers here.

Of course the first try will still produce the confusing error (that has nothing to do with how you write paths). With that in mind, you can make the hilariously horrible:

from playsound import playsound
try:
  playsound(mp3path)
except:
  playsound(mp3path)

or, only at the beginning of your script:

from playsound import playsound
try:
  playsound(mp3path)
except:
  pass

...since every other instance will work fine.

Or downgrade with pip install playsound==1.2.2, as suggested by Himanshu. It just works, without hacks.

Minty
  • 78
  • 1
  • 7
3

Just don't add these \\ every time, add them only after the name of the drive. And be sure to add r before the string.T he code should be like this

playsound(r'C:\\Users\Lenovo\OneDrive\Documents\Zoom\1. Chapter 1\play.mp3\play.mp3')

A raw-string is a string literal (prefixed with an r) in which the normal escaping rules have been suspended so that everything is a literal. The only character that is parsed within a regular string is the backslash, which must be followed by an escapable character (n, r, t etc.).

An escapable character means like a \n in a string and print it so it will mean that the rest of the strings should be printer in a new line. Thank me later!

3

Just replace double backslash "//" with a double forward slash "\\".

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
0

I have a solution for this problem. Just go to path where is your mp3 located. just right click and open visual studio and create new file and name it 01_sample_mp3.py

after that type command

from playsound import playsound
playsound('nameofmp3.mp3')

The execute the code.

enter image description here