Search code examples
python-2.7terminalpopenquicktime

python - open file in application


I am using terminal to sucsessfully open a file in quicktime player 7, but can't seem to get it working using python to do the same thing. So this is working from terminal:

open -a "Quicktime Player 7" /Users/Me/Movies/test.mov

But this is not working in python 2.7, it opens quicktime, but not the file:

command = ('open -a "Quicktime Player 7"', 'Users/Me/Movies/test.mov')
subprocess.Popen(command, shell=True)

What am I doing wrong?


Solution

  • If you pass command as list/tuple, you have to split the arguments up correctly:

    command = ('open', '-a', 'Quicktime Player 7', '/Users/Me/Movies/test.mov')
    subprocess.Popen(command, shell=True)
    

    Then I think you should also be able to drop the shell=True parameter. Further, you could look into subprocess.call() or subprocess.check_call() (the former returns the return value of the program, the latter raises an exception if the return value indicates an error):

    subprocess.check_call(['open', '-a', 'Quicktime Player 7', '/Users/Me/Movies/test.mov'])
    

    NB: Coding-style-wise, command is usually passed as list, as seen in the docs I linked above.

    Edit: Add '/' at the beginning of both paths to make it work.