Python中判断当前操作系统的几种方法

 更新时间:2025年05月14日 09:19:12   作者:知来者逆  
这篇文章主要为大家详细介绍了Python中判断当前操作系统的三种方法,主要是os.name,sys.platform和platform.system(),下面小编就来和大家详细讲讲具体操作吧

本文介绍 Python 判断操作系统的3种方法。以下的方法将分为这几部分:

  • Python os.name
  • Python sys.platform
  • Python platform.system()

Python os.name

Python 判断操作系统的方法可以使用 os.name,这里以 Python 3 为例,os.name 会返回 posix、nt、java 这几种结果。使用前需要先 import os。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
print(os.name)

# 在 Ubuntu 16.04 上的输出如下:
# posix

# 在 MacOS 10.15.7 上的输出如下:
# posix

# 在 Windows 10 上的输出如下:
# nt

在 os 模块下还有另一个 uname() 函数可以使用,uname() 会返回操作系统相关的版本信息。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
print(os.uname())

# 在 Ubuntu 16.04 上的输出如下:
# sysname='Linux', nodename='shengyu', release='4.10.0-40-generic', version='#44~16.04.1-Ubuntu SMP Thu Nov 9 15:37:44 UTC 2017', machine='x86_64'

# 在 MacOS 10.15.7 上的输出如下:
# posix.uname_result(sysname='Darwin', nodename='shengyudeMacBook-Pro.local', release='19.6.0', version='Darwin Kernel Version 19.6.0: Thu Sep 16 20:58:47 PDT 2021; root:xnu-6153.141.40.1~1/RELEASE_X86_64', machine='x86_64')

# Windows 下没有 os.uname()

sys.platform 有更细的分类,下一节会介绍。

Python sys.platform

sys.platform 返回的结果有以下几种情况:

  • AIX: 'aix'
  • Linux: 'linux'
  • Windows: 'win32'
  • Windows/Cygwin: 'cygwin'
  • macOS: 'darwin'

如果要用 sys.platform 判断操作系统,可以使用 startswith(),像 linux 与 linux2 的情况就可以被包含在以 linux 开头的字符串,写在同一个条件式里。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys

if sys.platform.startswith('linux'):
    print('Linux')
elif sys.platform.startswith('darwin'):
    print('macOS')
elif sys.platform.startswith('win32'):
    print('Windows')

Python platform.system()

Python 判断操作系统的方法可以使用 platform.system() 函数,platform.system() 会返回操作系统的名称,例如:Linux、Darwin、Java、Windows 这几种。如果无法判断操作系统的话会返回空字符串。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import platform

print(platform.system())
print(platform.release())

# 在 Ubuntu 16.04 上的输出如下:
# Linux
# 4.10.0-40-generic

# 在 MacOS 10.15.7 上的输出如下:
# Darwin
# 19.6.0

# 在 Windows 10 上的输出如下:
# Windows
# 10

方法补充

python判断当前系统是linux、windows还是MacOS

使用sys模块

import sys
 
if sys.platform.startswith("win"):
    print("当前系统是Windows")
elif sys.platform.startswith("linux"):
    print("当前系统是Linux")
elif sys.platform.startswith("darwin"):
    print("当前系统是Mac OS")
else:
    print("当前系统是其他操作系统")

sys.platform 会返回当前系统平台的标识符,Linux 是 ‘linux’、Windows 是 ‘win32’ 或者 ‘win64’、macOS 是 ‘darwin’,可以使用 startswith() 函数来进行判断。

使用platform模块

import platform
 
system = platform.system()
if system == "Windows":
    print("当前系统是Windows")
elif system == "Linux":
    print("当前系统是Linux")
elif system == "Darwin":
    print("当前系统是Mac OS")
else:
    print("当前系统是其他操作系统")

使用os模块

import os

system = os.name
if system == "nt":
    print("当前系统是Windows")
elif system == "posix":
    print("当前系统是Linux或Mac OS")
else
    print("当前系统是其他操作系统")

Python判断操作系统类型

方法一

import platform

def TestPlatform():
    print ("----------Operation System--------------------------")
    #Windows will be : (32bit, WindowsPE)
    #Linux will be : (32bit, ELF)
    print(platform.architecture())

    #Windows will be : Windows-XP-5.1.2600-SP3 or Windows-post2008Server-6.1.7600
    #Linux will be : Linux-2.6.18-128.el5-i686-with-redhat-5.3-Final
    print(platform.platform())

    #Windows will be : Windows
    #Linux will be : Linux
    print(platform.system())

    print ("--------------Python Version-------------------------")
    #Windows and Linux will be : 3.1.1 or 3.1.3
    print(platform.python_version())

def UsePlatform():
  sysstr = platform.system()
  if(sysstr =="Windows"):
    print ("Call Windows tasks")
  elif(sysstr == "Linux"):
    print ("Call Linux tasks")
  else:
    print ("Other System tasks")

UsePlatform()

方法二

import platform


def TestPlatform():
    return platform.architecture()[0]


def mysubData(subData):
    b = []

    for i in subData:
        try:
            if numpy.isnan(i):
                i = "Null"
                b.append(i)
            if TestPlatform() == "64bit":
                if isinstance(i, numpy.float64):
                    b.append(i)
                elif isinstance(i, numpy.int64):
                    b.append(i)
            elif TestPlatform() == "32bit":
                if isinstance(i, numpy.float32):
                    b.append(i)
                elif isinstance(i, numpy.int32):
                    b.append(i)
        except:
            if isinstance(i, str):
                if "'" in i:
                    i.replace("'", "")
                elif "%" in i:
                    i.replace("'", "")
                elif "\\" in i:
                    i.replace("'", "")
                b.append(i)
            else:
                b.append(i)

    return b

到此这篇关于Python中判断当前操作系统的几种方法的文章就介绍到这了,更多相关Python判断操作系统内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • python爬虫项目设置一个中断重连的程序的实现

    python爬虫项目设置一个中断重连的程序的实现

    这篇文章主要介绍了python爬虫项目设置一个中断重连的程序的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • pip升级pip3的快速方法指南

    pip升级pip3的快速方法指南

    使用python时经常使用到pip命令,可以方便安装python的各种第三方库这篇文章主要给大家介绍了关于pip升级pip3的快速方法,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-12-12
  • 10分钟学会使用python实现人脸识别(附源码)

    10分钟学会使用python实现人脸识别(附源码)

    这篇文章主要介绍了10分钟学会使用python实现人脸识别(附源码),帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下
    2021-03-03
  • Python堆栈的具体使用

    Python堆栈的具体使用

    本文主要介绍了Python堆栈的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-11-11
  • Python大数据之从网页上爬取数据的方法详解

    Python大数据之从网页上爬取数据的方法详解

    这篇文章主要介绍了Python大数据之从网页上爬取数据的方法,结合实例形式详细分析了Python爬虫爬取网页数据的相关操作技巧,需要的朋友可以参考下
    2019-11-11
  • python执行系统命令后获取返回值的几种方式集合

    python执行系统命令后获取返回值的几种方式集合

    今天小编就为大家分享一篇python执行系统命令后获取返回值的几种方式集合,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-05-05
  • Python编程语言的35个与众不同之处(语言特征和使用技巧)

    Python编程语言的35个与众不同之处(语言特征和使用技巧)

    这篇文章主要介绍了Python编程语言的35个与众不同之处,Python编程语言的语言特征和使用技巧,需要的朋友可以参考下
    2014-07-07
  • 对命令行模式与python交互模式介绍

    对命令行模式与python交互模式介绍

    今天小编就为大家分享一篇对命令行模式与python交互模式介绍,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-05-05
  • Python request中文乱码问题解决方案

    Python request中文乱码问题解决方案

    这篇文章主要介绍了Python request中文乱码问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • Python3监控疫情的完整代码

    Python3监控疫情的完整代码

    这篇文章主要介绍了Python3监控疫情的完整代码,代码简单易懂,非常不错具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-02-02

最新评论