Python判断操作系统类型

来源:互联网 发布:第一军团永远忠诚知乎 编辑:程序博客网 时间:2024/06/05 22:54

本文作者为吕浪(Victor Lv),转载请注明出处。

python(2.7) check the os type running on:

1. use sys.platform:

windows:    >>> import sys    >>> sys.platform    >>> 'win32'linux    >>> import sys    >>> sys.platform    >>> 'linux2'

2. use platform.system():

windows:    >>> import platform    >>> platform.system()    >>> 'Windows'linux:    >>> import platform    >>> platform.system()    >>> 'Linux'

3. 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

原创粉丝点击