pythonoperating-systemunamelsb

Is there a pythonic way to check whether OS is a 64bit Ubuntu?


Is there a pythonic way to check whether OS is a 64bit Ubuntu?

Currently, I've been doing it as such:

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)

Solution

  • You can use the platform module to get distribution and processor information:

    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)