python-在默认程序(Linux)中打开wav文件

python-在默认程序(Linux)中打开wav文件,python,audio,tkinter,Python,Audio,Tkinter,我想在默认程序中打开.wav文件。但它不起作用。这是我的代码: audiofile=(myFile[index]+".wav") # I have all files in array (without ".wav") try: try: os.system('xdg-open audiofile') except: os.system('start audiofile') except: pri

我想在默认程序中打开.wav文件。但它不起作用。这是我的代码:

audiofile=(myFile[index]+".wav") # I have all files in array (without ".wav") 

try:
    try:
        os.system('xdg-open audiofile')
    except:
        os.system('start audiofile')                    
except:
    print "error"

我没有发现任何错误,但它不起作用。我怎样才能解决它?谢谢。

您没有将音频文件的名称替换到操作系统命令中,因此它不可能工作

你需要一些类似的东西:

os.system('xdg-open ' + audiofile)
这假设您有一个与
.wav
文件关联的默认应用程序,当然您可以通过手动尝试您的命令来测试它


您可能还想检查
os.system
的返回值是否有错误代码,而不是依赖异常。

首先,您应该将变量
audiofile
填充到命令中,而不是字符串
'audiofile'
本身

os.system('xdg-open %s' % audiofile)
第二,, 当系统中不存在
xdg open
start
时,
os.system
不会引发异常。 首先通过以下步骤确定系统的类型:

如果我在终端“xdg open”path_to_audiofile”中键入,它会工作。但是如果我想播放python程序中的音频,我会出错。我不知道问题出在哪里。
>>> import platform
>>> platform.system()
'Linux'