Python Forum
Is there a better way of formatting os.uname() output?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there a better way of formatting os.uname() output?
#1
I'm currently trying to output the results from os.uname() to a Python window containing a QLabel.

The label requires the data as a string. I know various ways of converting the result into a string, but what I'd like to do is make the output look nice. Specifically:
  • Place each output field (sysname, release, etc.) onto a separate line.
  • Remove the commas.
  • Remove the opening and closing parentheses

Doing str(os.uname()) just gives one long line.

.replace() lets me insert newlines in place of the commas, but I'd have to use two additional .replace() calls to remove the opening and closing parentheses.

My best solution uses f-strings like so:

self.info = (f"{os.uname().sysname}\n\n"
             f"{os.uname().release}\n\n"
             f"{os.uname().version}\n\n"
              f"{os.uname().machine}"
              )

# Including this bit for context's sake.

self.text = QtWidgets.QLabel(self.info, alignment=QtCore.Qt.AlignCenter)
But that still seems rather clunky to me. Is there a better way of doing this, or should I just suck it up and use a loop?
Reply
#2
for item in (list(os.uname())):
    print(item)
Reply
#3
If can write cross platform try to do that,os.uname only work on Linux.
With platform should be able to get same info.
Can make a list and use getattr with those strings as attribute access for platform.
import platform

os_info = ['machine', 'version', 'system', 'release']
for item in os_info:
    print(getattr(platform, item)()) 
Output:
AMD64 10.0.19041 Windows 10
On Linux Repl.it
Output:
x86_64 #19~20.04.1-Ubuntu SMP Thu Aug 12 05:25:25 UTC 2021 Linux 5.11.0-1017-gcp
Reply
#4
(Aug-27-2021, 05:56 PM)snippsat Wrote: If can write cross platform try to do that,os.uname only work on Linux.

This isn't an issue as what I'm planning on making is for Unixes only.

At any rate, Larz60+'s post put me on the right track. The previous f-string code was replaced with the following:

self.info = ""
for item in os.uname():
    self.info += item + "\n\n"
Reply
#5
Untested as I'm on windows
self.info = "\n\n".join(name for name in os.uname())
Reply
#6
I don't think the comprehension is necessary. You can iterate directly over os.uname().

self.info = "\n\n".join(os.uname()) + "\n\n"
CodeWolf and Yoriz like this post
Reply
#7
(Aug-27-2021, 09:35 PM)bowlofred Wrote: I don't think the comprehension is necessary. You can iterate directly over os.uname().

self.info = "\n\n".join(os.uname()) + "\n\n"

Even better, thank you! ^^
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Formatting float number output barryjo 2 924 May-04-2023, 02:04 PM
Last Post: barryjo
  Formatting to output file Mark17 2 1,323 Jan-13-2022, 09:06 PM
Last Post: BashBedlam
  Is there a better way to print uname information? EkaCaesium 4 3,734 Apr-26-2021, 12:51 PM
Last Post: EkaCaesium
  Output formatting Steffenwolt 0 2,170 Jan-18-2018, 03:47 PM
Last Post: Steffenwolt

Forum Jump:

User Panel Messages

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