alvas
alvas

Reputation: 121992

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)

Upvotes: 1

Views: 71

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1121186

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)

Upvotes: 3

nitzanms
nitzanms

Reputation: 1718

Use the functionality provided by the platform module, specifically platform.architecture and platform.uname.

Upvotes: 0

Related Questions