Python判断操作系统类型
in 技术博客 with 0 comment

Python判断操作系统类型

in 技术博客 with 0 comment

python(2.7) check the os type running on:

use sys.platform:

windows:

    >>> import sys
    >>> sys.platform
    >>> 'win32'

linux

    >>> import sys
    >>> sys.platform
    >>> 'linux2'

use platform.system():

windows:

    >>> import platform
    >>> platform.system()
    >>> 'Windows'

linux:

    >>> import platform
    >>> platform.system()
    >>> 'Linux'

Judge the os type:

We can use "=="(equal) to judge whether the os is window or linux, or we can use string.startwith() function to check it:

if sys.platform.startswith('freebsd'):
    # FreeBSD-specific code here...
elif sys.platform.startswith('linux'):
    # Linux-specific code here...

Reference:
platform — Access to underlying platform’s identifying data

Responses