python - Linux 发行版名称解析

标签 python regex linux

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

ls /etc/*release

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

/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

但这会打印与 distroRelease 中相同的字符串。

最佳答案

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

>>> import platform
>>> platform.linux_distribution()
('Red Hat Enterprise Linux Server', '5.1', 'Tikanga')

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

>>> import platform
>>> platform.dist()
('redhat', '5.1', 'Tikanga')

关于python - Linux 发行版名称解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2756873/

相关文章:

python - 如何使用 python 中的 mord 模块进行序数回归?

python - 从元素列表中提取匹配的数字

mac地址上的Javascript正则表达式

Python正则表达式替换以创建笑脸

c++ - 我们如何确保缓存以减少 SQLite 数据库的文件系统写入周期

linux - x86 程序集 : Before Making a System Call on Linux Should You Save All Registers?

python - 来自其他类的方法的类装饰器

python - 在 Python 中访问数组列的最佳方法是什么?

python - 在Python中使用execute()/executemany()执行SQL语句

linux - 如何通过数值约束过滤 UNIX stdout?