Python: What OS am I running on?

前端 未结 26 2060
野趣味
野趣味 2020-11-22 05:44

What do I need to look at to see whether I\'m on Windows or Unix, etc?

26条回答
  •  野性不改
    2020-11-22 06:08

    If you not looking for the kernel version etc, but looking for the linux distribution you may want to use the following

    in python2.6+

    >>> import platform
    >>> print platform.linux_distribution()
    ('CentOS Linux', '6.0', 'Final')
    >>> print platform.linux_distribution()[0]
    CentOS Linux
    >>> print platform.linux_distribution()[1]
    6.0
    

    in python2.4

    >>> import platform
    >>> print platform.dist()
    ('centos', '6.0', 'Final')
    >>> print platform.dist()[0]
    centos
    >>> print platform.dist()[1]
    6.0
    

    Obviously, this will work only if you are running this on linux. If you want to have more generic script across platforms, you can mix this with code samples given in other answers.

提交回复
热议问题