Python Forum
Is there a better way to print uname information?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there a better way to print uname information?
#1
I'm currently trying to print the uname() results from the os module in a nice, readable manner.

Printing the uname() results directly like so isn't very readable:
from os import uname

os_info = uname()
print(os_info)

# Output
# posix.uname_result(sysname='Linux', (nodename withheld), release='5.4.0-72-generic',
# version='#80-Ubuntu SMP Mon Apr 12 17:35:00 UTC 2021', machine='x86_64')
I'd like to get the output to look something like this:

sysname: (sysname's value)
nodename: (nodename's value)
release: (release's value)
version: (version's value)
machine: (machine's value)

But so far the only way I can get output like that is by doing this:

from os import uname

os_info = f"""
sysname:  {uname().sysname} \n
nodename: {uname().nodename} \n
release: {uname().release} \n
version: {uname().version} \n
machine: {uname().machine}
"""
Which is clunky. I know that uname() returns an object you can iterate over (__iter__ is listed as an available method). But if I try looping through os_info and printing the result, I only get the values, the field names themselves (sysname, nodename, etc.) aren't available.

So I'd really like to know if there's some way to get both the field names and their values and print both out via a loop.

Sorry if my post is a bit crummy. Wall
Reply
#2
There's no easy (non-clunky) way that I can see. The names are attributes of the object, but not the only attributes. And there's no __dict__ attribute that can be cleanly inspected. I'd probably keep a list of the fields and loop over them. As the attributes are defined in the documentation, that seems the cleanest way to me.

from os import uname

uname_fields = ['sysname', 'nodename', 'release', 'version', 'machine']

u = uname()
for field in uname_fields:
    print(f"{field} => {getattr(u, field)}")
But if you really wanted to see a way not to do that, you could grab the attributes and ignore the ones that have underscores or are methods... Doesn't seem very stable to me, but it is currently functional.

from os import uname

u = uname()
for field in (
    x for x in dir(u) if "_" not in x and not callable(getattr(u, x))
):
    print(f"{field} => {getattr(u, field)}")
Reply
#3
You can also zip
>>> import os
>>> uname_fields = ('sysname', 'nodename', 'release', 'version', 'machine')
>>> list(zip(uname_fields, os.uname()))
[('sysname', 'Linux'), ('nodename', 'Aspire-X3-710'), ('release', '5.8.0-50-generic'), ('version', '#56~20.04.1-Ubuntu SMP Mon Apr 12 21:46:35 UTC 2021'), ('machine', 'x86_64')]
Reply
#4
in Linux

import distro
import platform

print("Linux Version:", distro.linux_distribution()[0], distro.linux_distribution()[1], distro.linux_distribution()[2])
print("Architektur:", platform.architecture()[0], platform.processor())
print("Kernel:", platform.release())
Output:
Linux Version: Linux Mint 20.1 ulyssa Architektur: 64bit x86_64 Kernel: 5.4.0-72-generic
Reply
#5
Thanks all! I decided to go with a slightly modified version bowlofred's solution:

uname_fields = ["sysname", "nodename", "release", "version", "machine"]
u = uname()
for field in uname_fields:
    print(f"{field.capitalize()}: {getattr(u, field)}")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a better way of formatting os.uname() output? CodeWolf 6 3,610 Aug-27-2021, 11:41 PM
Last Post: CodeWolf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020