What is problem in this python code? cannot specify extra characters after a string enclosed in quotation marks
Asked Answered
P

5

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

Posset answered 25/7, 2021 at 12:43 Comment(3)
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.Jittery
Thank you I found the solutionPosset
This bug bug is explained in this answer and this one.Scabies
B
19

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/

Benempt answered 18/9, 2021 at 18:46 Comment(0)
G
6

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.

Greenleaf answered 24/5, 2022 at 20:16 Comment(2)
how on earth did you figure this out?Bouley
@SidharthGhoshal I opened Python shell to experiment, and tried the same line without changing the path var.Greenleaf
M
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!

Modulate answered 26/8, 2021 at 11:46 Comment(0)
N
3

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

Nucleo answered 27/8, 2021 at 20:4 Comment(2)
Doesnt work mateCarrousel
This replacement, combined with an 'r' in front, worked for me. Although the backslash and forward slash are mislabeled above.Gardant
B
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

Bichloride answered 12/8, 2021 at 10:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.