python - 有没有一种Pythonic方法来检查操作系统是否是64位Ubuntu?

标签 python operating-system uname lsb

有没有一种Pythonic方法来检查操作系统是否是64位Ubuntu?

目前,我一直在这样做:

import os

def check_is_linux(distro, architecture, err_msg):
    try:
        this_os = os.popen('lsb_release -d').read()
        this_arch = os.popen('uname -a').read()
        assert distro in this_os and architecture in this_arch, err_msg
    except:
        print(err_msg)

def check_is_64bit_ubuntu(err_msg):
    check_is_linux('Ubuntu', 'x86_64', err_msg)

最佳答案

您可以使用platform module获取发行版和处理器信息:

import platform

def is_linux(distro, architecture):
    if not platform.system() == 'Linux':
        return False

    if platform.linux_distribution()[0].lower() != distro:
        return False

    return platform.processor() == architecture

def is_64bit_ubuntu():
    return is_linux('ubuntu', 'x86_64')

if not is_64bit_ubuntu():
    print(err_msg)

关于python - 有没有一种Pythonic方法来检查操作系统是否是64位Ubuntu?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31785850/

相关文章:

python - 检查字典键是否存在列表元素,如果存在则创建值列表,否则为 'None'

python - 从Python列表中的名称(字符串)访问对象属性

linux - 如何修改 Linux 内核以更改 uname 返回的版本字符串?

cmake - 如何去除 CMake 变量中的尾随空格?

python - Pandas 与另一列的最大值聚合分组?

python - 为 Web 应用程序实现张量分解的最有效语言

c - 在 xv6 中一个进程可以获得(分配)多大(多少内存)?

memory - DMA 如何影响 CPU?

java - 环境变量无法识别

linux - 为什么在我的 .bashrc 中使用 `uname` 时出现错误?