Skip to main content
 首页 » 编程设计

python之如何在 Python 脚本中可靠地检查我是否高于某个 CentOS 版本(CentOS 7)

2025年05月04日14youxin

我接到一项任务,要将我们的一系列内部应用程序从 CentOS6 移植到 CentOS7。通过这一举措,我们正在将依赖项从我们自己重新打包的外部包更改为包的官方上游版本。

因此,我正在寻找一段可靠的 python2.7 代码来执行此操作:

if CentOS version >= 7: 
    do things the new way 
else: 
    do things the deprecated way 

它将用于自动生成 .spec 文件以制作 RPM。

我一直在研究诸如解析 /etc/redhat-release 之类的东西,但这对于我想要的东西来说似乎有点不可靠。有没有更好的办法?

非常感谢。

请您参考如下方法:

你也可以试试:

In [1]: import platform 
 
In [2]: platform.linux_distribution() 
Out[2]: ('Red Hat Enterprise Linux Server', '6.5', 'Santiago') 
 
In [3]: dist = platform.linux_distribution() 
 
In [4]: "Red Hat" in dist[0] and dist[1].split('.')[0] == '6' 
Out[4]: True 
 
In [5]: 

第一个