Is there a better way of formatting os.uname() output? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Is there a better way of formatting os.uname() output? (/thread-34742.html) |
Is there a better way of formatting os.uname() output? - CodeWolf - Aug-27-2021 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:
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? RE: Is there a better way of formatting os.uname() output? - Larz60+ - Aug-27-2021 for item in (list(os.uname())): print(item) RE: Is there a better way of formatting os.uname() output? - snippsat - Aug-27-2021 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)()) On Linux Repl.it
RE: Is there a better way of formatting os.uname() output? - CodeWolf - Aug-27-2021 (Aug-27-2021, 05:56 PM)snippsat Wrote: If can write cross platform try to do that, 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" RE: Is there a better way of formatting os.uname() output? - Yoriz - Aug-27-2021 Untested as I'm on windows self.info = "\n\n".join(name for name in os.uname()) RE: Is there a better way of formatting os.uname() output? - bowlofred - Aug-27-2021 I don't think the comprehension is necessary. You can iterate directly over os.uname(). self.info = "\n\n".join(os.uname()) + "\n\n" RE: Is there a better way of formatting os.uname() output? - CodeWolf - Aug-27-2021 (Aug-27-2021, 09:35 PM)bowlofred Wrote: I don't think the comprehension is necessary. You can iterate directly over os.uname(). Even better, thank you! ^^ |