【问题标题】:Is there a pythonic way to check whether OS is a 64bit Ubuntu?有没有一种 Pythonic 方法来检查操作系统是否是 64 位 Ubuntu?
【发布时间】:2015-10-25 11:24:03
【问题描述】:

有没有一种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)

【问题讨论】:

    标签: python operating-system uname lsb


    【解决方案1】:

    您可以使用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)
    

    【讨论】:

    • 谢谢!! platform 太棒了!!但是小错误,应该是is_linux('ubuntu', 'x86_64'),因为platform.linux_distribution()[0].lower()
    • @alvas:啊,是的,我的错。很高兴我能帮上忙!
    【解决方案2】:

    使用platform 模块提供的功能,特别是platform.architecture 和platform.uname。

    【讨论】:

      猜你喜欢
      • 2017-11-23
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 2012-06-24
      • 2011-10-06
      • 2019-04-04
      • 2012-08-26
      相关资源
      最近更新 更多