Linux发行版名称解析

Max*_*rai -1 python regex linux

我选择这种方式获取linux发行版名称:

ls /etc/*release
Run Code Online (Sandbox Code Playgroud)

现在我必须解析它的名字:

/etc/<name>-release

def checkDistro():
    p = Popen('ls /etc/*release' , shell = True, stdout = PIPE)
    distroRelease = p.stdout.read()

    distroName = re.search( ur"\/etc\/(.*)\-release", distroRelease).group()
    print distroName
Run Code Online (Sandbox Code Playgroud)

但这会打印出distroRelease中的相同字符串.

Dav*_*yan 7

另一种方法是使用内置方法platform.linux_distribution()(Python 2.6+中提供):

>>> import platform
>>> platform.linux_distribution()
('Red Hat Enterprise Linux Server', '5.1', 'Tikanga')
Run Code Online (Sandbox Code Playgroud)

在旧版本的Python中,platform.dist()可以使用:

>>> import platform
>>> platform.dist()
('redhat', '5.1', 'Tikanga')
Run Code Online (Sandbox Code Playgroud)


Amb*_*ber 5

你需要.group(1),因为你想要第一个捕获组 - 没有参数,它默认为.group(0)整个匹配的文本.