python-3.xubuntupopen

Popen does not raise exception


Popen is not raising an exception and so am unable to catch it. Am I missing anything? It prints the error on terminal window as if it succeded. Refer picture. However it lauches properly if valid argument is given.

import subprocess
try:
    subprocess.Popen(["xdg-open", "invalid"])
    print("Launched")
except:
    print("Exception")

https://photos.app.goo.gl/G6xWDstUWuL8Py3V6

Environment: Python 3.10.12 [GCC 11.4.0], Ubuntu 22.04.4 LTS


Solution

  • Found the answer I was looking for:

    Popen exception example

    Need to do something like this:

    import subprocess
    
    try:
        res = subprocess.Popen(["xdg-open", "/home/sandeep/Scripts/ControlPanel/Data/ControlPanel.ini"],stdout=subprocess.PIPE,stderr=subprocess.PIPE)  
        output,error = res.communicate()
        if output:
            print(" ret> ",res.returncode)
            print(" OK> output ",output)
        if error:
            print(" ret> ",res.returncode)
            print(" Error> error ",error.strip())
    except:
        print("Exception")