Python: how to determine a specific Linux distribution?

I would like to know how to determine the exact Linux distribution I participate in (excluding version numbers) from within the Python script and define the variable as equal to it. Now I have to clarify and say that I saw these two questions:

and not one of them was useful to me, because the first of these questions had answers that were very generalized and simply returned posixfor all Linux distributions. The answers to the second question did not help, as I sometimes work on some more obscure distributions such as Manjaro Linux and Sabayon Linux. The most applicable answer to the second question was platform.linux_distribution()which returns to Manjaro:

('', '', '')

which, as you can see, does not help. Now I know a way that I will get halfway an acceptable answer, for example:

from subprocess import call
call(["lsb_release", "-si"])

returns the result (on Manjaro Linux, of course):

ManjaroLinux
0

but defining a variable:

a=call(["lsb_release", "-si"])

gives the value a with the value:

>>> a
0
+4
source share
5 answers
In [24]: open('/etc/lsb-release').readline().strip().split('=')[-1]
Out[24]: 'LinuxMint'
+1
source

"a" is just the exit status, try:

from subprocess import Popen, PIPE, STDOUT
cmd = "lsb_release -si"
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
output = p.stdout.read()
+4
source

check_output.

from subprocess import check_output
a = check_output(["lsb_release", "-si"])
+2

open("/etc/issue","r").read()?

+1

subprocess.check_output. : " ". : https://docs.python.org/2/library/subprocess.html

:

a = subprocess.check_output(["lsb_release", "-si"])

:

'Ubuntu\n'
0

Source: https://habr.com/ru/post/1624287/


All Articles