0

I'm trying to check if output of 'os' contains specific words and execute commands based on the results.

The problem is that the program tries to execute the commands even if output of 'os' does not contain those words.

What is the proper way to write it? These are the clauses that check for it:

elif int(choice0) == 3:
    import platform
    os = platform.linux_distribution()

    if "Fedora" in os:
        call(["sudo", "dnf", "install", "android-tools"])
        exit()

    elif "Mint" or "Ubuntu" or "Debian" in os:
        call(["sudo", "apt", "install", "android-tools-adb", "android-tools-fastboot"])
        exit()

    elif "Manjaro" or "Antergos" or "Arch" in os:
        call(["sudo", "pacman", "-S", "android-tools"])
        exit()

Thanks everyone for trying to help, my current code looks like this:

        import platform
        distro = platform.linux_distribution()

        if distro in ("Fedora", ):
            call(["sudo", "dnf", "install", "android-tools"])
            exit()

        elif distro in ("Ubuntu" or "Mint" or "Debian"):
            call(["sudo", "apt", "install", "android-tools-adb", "android-tools-fastboot"])
            exit()

        elif distro in ("Arch" or "Antergos" or "Manjaro"):
            call(["pacman", "-S", "android-tools"])
            exit()

I tried three different solutions and none of them actually worked.

metaphorex0
  • 9
  • 1
  • 3
  • 1
    Don't call a variable `os`! That is the name of a common module in the standard library and you will mask it. Not to mention confuse the heck out of people. – cdarke Aug 14 '16 at 18:23
  • By using ```set intersection``` you can check for the condition.```set(os).intersection(["Mint", "Ubuntu", "Debian"])``` – TrigonaMinima Aug 14 '16 at 18:33

1 Answers1

1

Using or the way you are, essentially evaluates the first value in the expression (here the literal "mint") and if that evaluates to True (it does, non-empty string literals do) it simply returns it thereby making the if check succeed.

You should be checking if any of those values are contained in the tuple returned from platform.linux_distribution. Your clauses should look like:

if any(i in os for i in {"manjaro", "antergos", "arch"})

which is a more compact way of correctly using chained ors:

if "manjaro" in os or "antergos" in os or "debian" in os:

That's the first issue, the second issue arises by the names you're using to check whether a distro is in os. The names you should be checking for should match those located in the _supported_dists tuple, namely:

>>> print(platform._supported_dists)
('SuSE', 'debian', 'fedora', 'redhat', 'centos', 'mandrake', 'mandriva', 'rocks', 
'slackware', 'yellowdog', 'gentoo', 'UnitedLinux', 'turbolinux', 'arch', 'mageia')

So, you shouldn't be capitalizing "arch" or "fedora" or "debian".

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
  • Now when I enter "3" to initialize it, it does not react to it at all, just nothing happens. – metaphorex0 Aug 14 '16 at 19:41
  • Sorry, been busy. My OS is Arch Linux. – metaphorex0 Aug 14 '16 at 22:22
  • @metaphorex0 you need to check the capitalization of the OS names you're using, see `platform._supported_dists`. By checking with a name of `"Arch"` will fail, you need to use `"arch"` – Dimitris Fasarakis Hilliard Aug 14 '16 at 22:29
  • I don't think I understand. I used this on my other machine and it gave the correct output even though the distro installed on that machine (Antergos) is not listed in `platform._suported_dists`. Also, I don't want it to check if the output contains specific distro. I want it to check if the output contains specific words (which in fact are the same). ` – metaphorex0 Aug 14 '16 at 22:35