问题
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?
回答1:
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.
回答2:
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.
回答3:
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])
回答4:
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.
回答5:
import mp3play,time
data= r'pathname'
clip = mp3play.load(data)
clip.play()
time.sleep(20)
clip.stop()
回答6:
# 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)
回答7:
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))
来源:https://stackoverflow.com/questions/3198604/can-python-open-a-mp3-file