简体   繁体   English

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

[英]python - open wav file in default program (Linux)

I want to open .wav file in default program. 我想在默认程序中打开.wav文件。 But it doesn´t work. 但它不起作用。 This is my code: 这是我的代码:

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"

I don´t get any error, but it doesn´t work. 我没有得到任何错误,但它不起作用。 How can I solve it? 我该如何解决? Thank you. 谢谢。

You aren't substituting the name of the audio file into your OS commands, so it can't possibly work. 您没有将音频文件的名称替换为您的OS命令,因此它无法工作。

You'd need something like: 你需要这样的东西:

os.system('xdg-open ' + audiofile)

This assumes that you have a default application associated with .wav files, which of course you can test by trying your command manually. 这假设您有一个与.wav文件关联的默认应用程序,当然您可以通过手动尝试命令来测试。

You might also want to check the return value of os.system for an error code, rather than relying on exceptions. 您可能还需要检查os.system的返回值以os.system错误代码,而不是依赖于异常。

First of all, you should fill the variable audiofile into the command, not the string 'audiofile' itself 首先,您应该将变量audiofile填入命令,而不是字符串'audiofile'本身

os.system('xdg-open %s' % audiofile)

Second, os.system will NOT throw an exception when xdg-open or start doesn't exist in system. 其次,当系统中不存在xdg-openstart时, os.system不会抛出异常。 Determine the type of system first by platform.system 首先通过platform.system确定系统类型

>>> import platform
>>> platform.system()
'Linux'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM