0
import webbrowser
import os
try:
    response=os.system('start notepad++.exe')
except OSError:
    webbrowser.open("https://www.rollapp.com/launch/notepad++")

If notepad++ is installed in system then it should open, otherwise the online notepad++ should launch.

However, try and except are not working in that way.

How does it work?

bunbun
  • 2,595
  • 3
  • 34
  • 52
  • Look at this question https://stackoverflow.com/questions/6466711/what-is-the-return-value-of-os-system-in-python. You will see that the `os` library doesn't have a standardized return (and thus isn't captured by your except clause) as it's OS dependant. Furthermore, i'd be extremely annoyed if your program opens another program of mine (if I have it) or forces me to a website, you should seriously consider another approach. – Bas Jansen Dec 06 '18 at 09:34
  • See also this question: https://gis.stackexchange.com/questions/86522/os-system-subprocess-call-error-handling-when-error-in-command-line-function. Seems like you can use subprocess.call() instead, which will return an OSError – Plasma Dec 06 '18 at 09:35
  • `subprocess` is indeed the way to go, it is based on `popen2` and offers proper exception support, etc. – Bas Jansen Dec 06 '18 at 09:48
  • 1
    `os.system` doesn't raise exceptions on failure. Consider consulting the documentation when you think things behave unexpectedly. – molbdnilo Dec 06 '18 at 09:54

2 Answers2

0

That is because OS doesn't throw exceptions as it is (as expected) very OS dependant, instead it returns an exit code (0) which is clearly described in the documentation. Instead, you should consider using subprocess.call() which offers a superset around it (popen). For instance the following will work (I'll let you worry about locating the notepad++ installation location, hint).

import webbrowser
import subprocess
try:
    # NOTE: The hardcoded path
    response=subprocess.call('C:/Program Files (x86)/Notepad++/notepad++.exe')
except OSError:
    webbrowser.open("https://www.rollapp.com/launch/notepad++")
Bas Jansen
  • 3,273
  • 5
  • 30
  • 66
  • can't pass "C:/Program Files (x86)/Notepad++/notepad++.exe" as it is different for different users that's why we have to pass general path "start notepad++.exe" but in this it is not working – Rahul Chaudhary Dec 06 '18 at 18:29
  • @RahulChaudhary That is exactly why trying to run another program on someone's computer is something you shouldn't be doing. However, I have linked another question that goes into finding the installation location for a program (as it isn't what you asked). – Bas Jansen Dec 07 '18 at 10:44
  • actually i am trying to develop user interface of windows10 that's why i have to consider general path – Rahul Chaudhary Dec 07 '18 at 16:46
-1

try/except is for cases where your code execution throws some kind of Exception which is not happening in your example. Read more about this here

You could look for the value in response to see the exit status of the command you tried to execute. Normally if it returns 0, the command was executed successfully and otherwise not. Which means the code would then look like:

response=os.system('start notepad++.exe')
if response != 0:
    webbrowser.open("https://www.rollapp.com/launch/notepad++")
philoj
  • 468
  • 2
  • 13
  • Don't do this, this makes it very OS dependant – Bas Jansen Dec 06 '18 at 09:50
  • @BasJansen my bad. Should i delete this or leave it for others to see? – philoj Dec 06 '18 at 10:28
  • That is up to you, your answer will work in specific cases so it's not 'wrong' perse but it's not the ideal (imho). – Bas Jansen Dec 06 '18 at 10:38
  • when os.system('start notepad++.exe') execute it throws an exception through cmd then we have to handle it manually after that browser open is there any way to handle that exception – Rahul Chaudhary Dec 06 '18 at 16:31
  • 1
    If you need to handle exceptions on execution of the command, you must use @BasJansen 's method. Did you try replacing os.system('start notepad++.exe') call with subprocess.call('start notepad++.exe')? (Validity of that command is another issue) – philoj Dec 07 '18 at 09:09