8

Is it possible to open an mp3 file in Python (possible using Popen) and I don't mean to run it in the program I mean as a separate window in media player or whatever just for it to open it when I call the function and if so how?

thisisnotshort
  • 342
  • 3
  • 11
DonJuma
  • 2,028
  • 13
  • 42
  • 70
  • related: [Is there an platform independent equivalent of os.startfile()?](http://stackoverflow.com/q/17317219/4279) – jfs Jan 30 '15 at 22:24

7 Answers7

14

Opening a file with its associated application (Windows only):

import os
os.startfile('my_mp3.mp3')

A link to the documentation can be found here.

ChristopheD
  • 112,638
  • 29
  • 165
  • 179
  • it threw an error it said : Traceback (most recent call last): File "C:\Users\matt\Desktop\1.py", line 3, in os.startfile('C:\Users\matt\Desktop\test.mp3') WindowsError: [Error 2] The system cannot find the file specified: 'C:\\Users\\matt\\Desktop\test.mp3' – DonJuma Jul 07 '10 at 20:48
  • here is the script import os os.startfile('C:\Users\matt\Desktop\test.mp3') – DonJuma Jul 07 '10 at 20:50
  • @Matthew Carter: you probably need to double your backslashes (`import os ; os.startfile('C:\\Users\\matt\\Desktop\\test.mp3')` or use forward slash... – ChristopheD Jul 07 '10 at 20:53
  • wait never mind it worked i thought i needed to put the full path i guess not. – DonJuma Jul 07 '10 at 20:53
  • In the documentation I linked to it states `os.startfile` by default expects paths starting from the current working directory, although you *can* use absolute paths (the previous comment about the need to double your backslashes still holds true though). – ChristopheD Jul 07 '10 at 20:57
1

If you have vlc already installed on your system, then you can use the cvlc command.

import os
os.system('cvlc path/to/foo.mp3')

That will work. Hope it helps.

padfoot27
  • 517
  • 9
  • 13
1

Here is the Python docs for Python in Music: http://wiki.python.org/moin/PythonInMusic

Listed there are libraries for opening and playing mp3, amongst other formats.

kobrien
  • 2,931
  • 2
  • 24
  • 33
1
import mp3play,time
data= r'pathname'
clip = mp3play.load(data)
clip.play()
time.sleep(20)
clip.stop()
1
# Just listen to all the mp3 files in order
import os
folder=os.listdir(os.getcwd())
for files in folder:
    if files.endswith(".mp3"):
        os.startfile(files)
PythonProgrammi
  • 22,305
  • 3
  • 41
  • 34
1

You could also use subprocess. Then you would have to specify the path to the executable you want to run, which might not be helpful if you want this to work on someone else's computer, but is generally quite a powerful technique.

Usage:

import subprocess
PLAYERPATH = "C:/Program Files (x86)/VideoLAN/VLC/vlc.exe"
subprocess.call([PLAYERPATH, FILEPATH])
dvdod
  • 11
  • 1
0

This script will pick a random song in the current directory. And will skip any file that is not a .mp3 file. You could add extra extensions to the list to be opened for example: ext3=['.mp3', '.mp4'] and so on.

import random,os,sys

folder=os.listdir(os.getcwd())
file=random.choice(folder)
ext3=['.mp3']
print('First random pick: '+file)

while file[-4:] not in ext3 :
    print('Not an MP3 file  : '+file)
    file=random.choice(folder)
else:
    os.startfile(file)
    print('Song name: '+file)

sys.exit()

##os.startfile(random.choice(folder))