Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

cross-platform launch command

By: a guest on Oct 25th, 2012  |  syntax: Python  |  size: 0.84 KB  |  views: 161  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import os
  2. import subprocess
  3. import sys
  4.  
  5. def launch(program):
  6.   """launch(program)
  7.    Run program as if it had been double-clicked in Finder, Explorer,
  8.    Nautilus, etc. On OS X, the program should be a .app bundle, not a
  9.    UNIX executable. When used with a URL, a non-executable file, etc.,
  10.    the behavior is implementation-defined.
  11.    
  12.    Returns something false (0 or None) on success; returns something
  13.    True (e.g., an error code from open or xdg-open) or throws on failure.
  14.    However, note that in some cases the command may succeed without
  15.    actually launching the targeted program."""
  16.   if sys.platform == 'darwin':
  17.     ret = subprocess.call(['open', program])
  18.   elif sys.platform.startswith('win'):
  19.     ret = os.startfile(os.path.normpath(program))
  20.   else:
  21.     ret = subprocess.call(['xdg-open', program])
  22.   return ret