gpt4 book ai didi

python - 什么是 sys.getwindowsversion() 的 linux 等价物

转载 作者:太空狗 更新时间:2023-10-30 02:30:01 24 4
gpt4 key购买 nike

没有 sys.getlinuxversion(),我觉得我可能必须从几个地方拼凑它(这很好);但是,也许不是?

import sys, os
from collections import namedtuple

## This defines an interface similar to the object that getwindowsversion() returns
## (I say 'similar' so I don't have to explain the descriptor magic behind it)
WindowsVersion = namedtuple('WindowsVersion', 'major minor build platform product_type service_pack service_pack_major service_pack_minor suite_mask')

## Looking for something that quacks like this:
LinuxVersion = namedtuple('LinuxVersion', 'major minor build whatever attrs are needed')

## To create something that quacks similarly to this (in case you're wondering)
PlatformVersion = namedtuple('PlatformVersion', 'major minor micro build info')

## EDIT: A newer version of Version, perhaps?
class Version:
_fields = None
def __init__(self, *subversions, **info)
self.version = '.'.join([str(sub) for sub in subversions])
self.info = info
instance_fields = self.__class__._fields
if instance_fields is not None:
if isinstance(instance_fields, str):
instance_fields = instance_fields.split()
for field in instance_fields:
if field in self.info:
setattr(self, field, self.info.pop(field))

## This makes the PlatformVersion (or any other Version) definition a mere:
class PlatformVersion(Version):
_fields = 'build' ## :)

## Now PlatformInfo looks something like:
class PlatformInfo:
def __init__(self, *version_number, platform=None, version_info=None, **info):
self.platform = sys.platform if platform is None else platform
self.info = info
if self.platform in ('win32', 'win64'):
works_great = sys.getwindowsversion()
self.version = PlatformVersion(works_great.major, works_great.minor, works_great.service_pack_major, build=works_great.build, **dict(version_info))
else:
self.version = your_answer(PlatformVersion(os.uname().release, build=os.uname().version, **dict(version_info))), 'hopefully'
self.version = self.version[0] ## lol

谢谢!

最佳答案

Linux(和所有类 Unix 系统)有 uname提供此类信息的系统调用:

>>> import os
>>> os.uname().release
'3.11.10-17-desktop'

显示内核版本。

请注意,在 Python 2 上它将返回元组而不是命名元组:

>>> import os
>>> os.uname()[2]
'3.11.10-17-desktop'

关于python - 什么是 sys.getwindowsversion() 的 linux 等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29645228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com