最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Python中判断当前系统是Windows还是Linux(Ubuntu等)还是Mac,以及获得对应的系统版本信息

Python crifan 10235浏览 0评论

【问题】

写了个脚本:BlogsToWordPress,里面后来加上了:

#------------------------------------------------------------------------------
def logRuntimeInfo():
    logging.info("Current runtime info:");
    logging.info("Paramenters       : %s", sys.argv);
    logging.info("Python version    : %s", sys.version_info);
    logging.info("Windows version   : %s", sys.getwindowsversion());
    logging.info("Default encoding  : %s", sys.getdefaultencoding());
    logging.info("Current path      : %s", sys.prefix);
    return;

结果却在Ubuntu中出现错误了:

AttributeError: ‘module’ object has no attribute ‘getwindowsversion’

 

现在希望,从Python中,能够判断当前系统是Windows还是Linux还是Mac,以及获得对应的系统的版本信息。

【解决过程】

1.参考:

How to check if OS is Vista in Python?

Python: What OS am I running on?

然后添加代码:

    if(getattr(sys, "getwindowsversion", None)):
        logging.info("Windows version   : %s", sys.getwindowsversion());
    
    logging.info("platform.system()=%s", platform.system());
    logging.info("platform.release()=%s", platform.release());
    logging.info("os.name=%s", os.name);
    logging.info("sys.platform=%s", sys.platform);

输出结果为:

LINE 1476 : INFO     Windows version   : sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1')
LINE 1478 : INFO     platform.system()=Windows
LINE 1479 : INFO     platform.release()=7
LINE 1480 : INFO     os.name=nt
LINE 1481 : INFO     sys.platform=win32

2.最后,再去参考python手册中,关于platform的介绍,最后使用其中的跨平台的函数,显示出一堆相关的信息,即可:

import platform;

    # ouput system type and version info
    logging.info("platform.machine()=%s", platform.machine());
    logging.info("platform.node()=%s", platform.node());
    logging.info("platform.platform()=%s", platform.platform());
    logging.info("platform.processor()=%s", platform.processor());
    logging.info("platform.python_build()=%s", platform.python_build());
    logging.info("platform.python_compiler()=%s", platform.python_compiler());
    logging.info("platform.python_branch()=%s", platform.python_branch());
    logging.info("platform.python_implementation()=%s", platform.python_implementation());
    logging.info("platform.python_revision()=%s", platform.python_revision());
    logging.info("platform.python_version()=%s", platform.python_version());
    logging.info("platform.python_version_tuple()=%s", platform.python_version_tuple());
    logging.info("platform.release()=%s", platform.release());
    logging.info("platform.system()=%s", platform.system());
    #logging.info("platform.system_alias()=%s", platform.system_alias());
    logging.info("platform.version()=%s", platform.version());
    logging.info("platform.uname()=%s", platform.uname());

 

当前系统输出的信息如下:

LINE 1476 : INFO     platform.machine()=AMD64
LINE 1477 : INFO     platform.node()=PC-CLI-1
LINE 1478 : INFO     platform.platform()=Windows-7-6.1.7601-SP1
LINE 1479 : INFO     platform.processor()=Intel64 Family 6 Model 42 Stepping 7, GenuineIntel
LINE 1480 : INFO     platform.python_build()=('default', 'Jun 12 2011 15:08:59')
LINE 1481 : INFO     platform.python_compiler()=MSC v.1500 32 bit (Intel)
LINE 1482 : INFO     platform.python_branch()=
LINE 1483 : INFO     platform.python_implementation()=CPython
LINE 1484 : INFO     platform.python_revision()=
LINE 1485 : INFO     platform.python_version()=2.7.2
LINE 1486 : INFO     platform.python_version_tuple()=('2', '7', '2')
LINE 1487 : INFO     platform.release()=7
LINE 1488 : INFO     platform.system()=Windows
LINE 1490 : INFO     platform.version()=6.1.7601
LINE 1491 : INFO     platform.uname()=('Windows', 'PC-CLI-1', '7', '6.1.7601', 'AMD64', 'Intel64 Family 6 Model 42 Stepping 7, GenuineIntel')

【总结】

利用python自带的platform库函数,(可以不用os,system等库了),打印一堆信息,即可获得相关的平台类型以及版本信息了。

转载请注明:在路上 » 【已解决】Python中判断当前系统是Windows还是Linux(Ubuntu等)还是Mac,以及获得对应的系统版本信息

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.252 seconds, using 22.06MB memory