Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何检查输出是否包含特定单词?_Python_Python 3.x_Subprocess - Fatal编程技术网

Python 如何检查输出是否包含特定单词?

Python 如何检查输出是否包含特定单词?,python,python-3.x,subprocess,Python,Python 3.x,Subprocess,我试图检查“os”的输出是否包含特定的字,并根据结果执行命令 问题是,即使“os”的输出不包含这些字,程序也会尝试执行这些命令 正确的写作方法是什么?以下是检查它的条款: elif int(choice0) == 3: import platform os = platform.linux_distribution() if "Fedora" in os: call(["sudo", "dnf", "install", "android-tools"])

我试图检查“os”的输出是否包含特定的字,并根据结果执行命令

问题是,即使“os”的输出不包含这些字,程序也会尝试执行这些命令

正确的写作方法是什么?以下是检查它的条款:

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()
感谢大家的帮助,我当前的代码如下:

        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()

我尝试了三种不同的解决方案,但没有一种真正起作用。

按照您的方式使用
,基本上计算表达式中的第一个值(此处为literal
“mint”
),如果计算结果为
True
(确实如此,则非空字符串literal会)它只返回它,从而使
检查成功

您应该检查从
platform.linux\u发行版
返回的元组中是否包含这些值中的
任何
。您的条款应如下所示:

if any(i in os for i in {"manjaro", "antergos", "arch"})
正确使用链式
s的更简洁的方法是:

if "manjaro" in os or "antergos" in os or "debian" in os:
这是第一个问题,第二个问题是您用来检查发行版是否在
os
中的名称。您应该检查的名称应与
\u supported\u dists
元组中的名称匹配,即:

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

因此,您不应该大写
“arch”
“fedora”
“debian”

不要调用变量
os
!这是标准库中公共模块的名称,您将屏蔽它。更不用说把人搞糊涂了。通过使用
set intersection
你可以检查条件。
set(os)。intersection([“Mint”、“Ubuntu”、“Debian”)
现在当我输入“3”初始化它时,它根本没有反应,只是什么都没有发生。对不起,我一直很忙。我的操作系统是Arch Linux。@ex0您需要检查正在使用的操作系统名称的大小写,请参阅
platform.\u supported\u dists
。如果使用
“Arch”
名称进行检查,则会失败,您需要使用
“Arch”
我想我不明白。我在我的另一台机器上使用了它,它提供了正确的输出,即使安装在该机器上的发行版(Antergos)未在
平台中列出。另外,我不希望它检查输出是否包含特定的发行版。我希望它检查输出是否包含特定的单词(实际上是相同的)`