Syntax error on else

I’m writing a gui program that will allow me to select apps and then click run to launch them (as a way to learn python) I’m using ubuntu 20.04 and keep running into a syntax error on the first “else” I can’t see what’s wrong though.

def runApps():
     for app in apps:
         if sys.platform == "win32":
             os.startfile(app)
             else:
                 opener = "open" if sys.platform == "darwin":
             else: "xdg-open"
             subprocess.call([opener, app])

It would help if your indentation showed, see the about posting for how to do that.

That being said, I think you want
opener = “open” if sys.platform == “darwin” else “xdg-open”
without the colons and on one line. This is not an if statement, but an if operator.

@Mholscher Thank you I will try that. Sorry for the poor formatting on my part. I corrected the formatting please let me know if that changes your response.

I am still getting the same syntax error on the first else

The else needs to be indented the same as the matching if.

def runApps():
    for app in apps:
        if sys.platform == "win32":
            os.startfile(app)
        else:
            opener = "open" if sys.platform == "darwin" else "xdg-open"
        subprocess.call([opener, app])
1 Like

this fixed the syntax completely. However when I click the button to run the program in the GUI, instead of launching the program it opens the .txt file of the program. (for reference I am using Ubuntu 20.04 so there is no exe files only .desktops which is what I am trying to launch with this code.)

Hi James,

I’m afraid you have lost me. You talk about “the GUI” but I have no idea
which GUI you are referring to. Linux systems support many different
desktop environments.: there is Gnome, Gnome2, XFCE, TDE, KDE, MATE, and
many more that I don’t recall the names of. I have no idea what Ubuntu
uses.

So you have a .desktop file, and you are clicking it (double-clicking?)
and an unrelated .txt file opens in a text editor? Sounds weird. But it
is definitely not a Python problem, it is something to do with your
desktop environment.

You might start by telling us what desktop environment you are using,
how you created the .desktop file, and what the contents of the desktop
file are. (Some .desktop files contain a ton of internationalised
strings; if that’s the case for yours, please just copy and paste the
English section.)