欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中判斷當(dāng)前操作系統(tǒng)的幾種方法

 更新時(shí)間:2025年05月14日 09:19:12   作者:知來者逆  
這篇文章主要為大家詳細(xì)介紹了Python中判斷當(dāng)前操作系統(tǒng)的三種方法,主要是os.name,sys.platform和platform.system(),下面小編就來和大家詳細(xì)講講具體操作吧

本文介紹 Python 判斷操作系統(tǒng)的3種方法。以下的方法將分為這幾部分:

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

Python os.name

Python 判斷操作系統(tǒng)的方法可以使用 os.name,這里以 Python 3 為例,os.name 會(huì)返回 posix、nt、java 這幾種結(jié)果。使用前需要先 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 模塊下還有另一個(gè) uname() 函數(shù)可以使用,uname() 會(huì)返回操作系統(tǒng)相關(guān)的版本信息。

#!/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 有更細(xì)的分類,下一節(jié)會(huì)介紹。

Python sys.platform

sys.platform 返回的結(jié)果有以下幾種情況:

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

如果要用 sys.platform 判斷操作系統(tǒng),可以使用 startswith(),像 linux 與 linux2 的情況就可以被包含在以 linux 開頭的字符串,寫在同一個(gè)條件式里。

#!/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 判斷操作系統(tǒng)的方法可以使用 platform.system() 函數(shù),platform.system() 會(huì)返回操作系統(tǒng)的名稱,例如:Linux、Darwin、Java、Windows 這幾種。如果無法判斷操作系統(tǒng)的話會(huì)返回空字符串。

#!/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

方法補(bǔ)充

python判斷當(dāng)前系統(tǒng)是linux、windows還是MacOS

使用sys模塊

import sys
 
if sys.platform.startswith("win"):
    print("當(dāng)前系統(tǒng)是Windows")
elif sys.platform.startswith("linux"):
    print("當(dāng)前系統(tǒng)是Linux")
elif sys.platform.startswith("darwin"):
    print("當(dāng)前系統(tǒng)是Mac OS")
else:
    print("當(dāng)前系統(tǒng)是其他操作系統(tǒng)")

sys.platform 會(huì)返回當(dāng)前系統(tǒng)平臺(tái)的標(biāo)識(shí)符,Linux 是 ‘linux’、Windows 是 ‘win32’ 或者 ‘win64’、macOS 是 ‘darwin’,可以使用 startswith() 函數(shù)來進(jìn)行判斷。

使用platform模塊

import platform
 
system = platform.system()
if system == "Windows":
    print("當(dāng)前系統(tǒng)是Windows")
elif system == "Linux":
    print("當(dāng)前系統(tǒng)是Linux")
elif system == "Darwin":
    print("當(dāng)前系統(tǒng)是Mac OS")
else:
    print("當(dāng)前系統(tǒng)是其他操作系統(tǒng)")

使用os模塊

import os

system = os.name
if system == "nt":
    print("當(dāng)前系統(tǒng)是Windows")
elif system == "posix":
    print("當(dāng)前系統(tǒng)是Linux或Mac OS")
else
    print("當(dāng)前系統(tǒng)是其他操作系統(tǒng)")

Python判斷操作系統(tǒng)類型

方法一

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

到此這篇關(guān)于Python中判斷當(dāng)前操作系統(tǒng)的幾種方法的文章就介紹到這了,更多相關(guān)Python判斷操作系統(tǒng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python爬蟲項(xiàng)目設(shè)置一個(gè)中斷重連的程序的實(shí)現(xiàn)

    python爬蟲項(xiàng)目設(shè)置一個(gè)中斷重連的程序的實(shí)現(xiàn)

    這篇文章主要介紹了python爬蟲項(xiàng)目設(shè)置一個(gè)中斷重連的程序的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • pip升級(jí)pip3的快速方法指南

    pip升級(jí)pip3的快速方法指南

    使用python時(shí)經(jīng)常使用到pip命令,可以方便安裝python的各種第三方庫(kù)這篇文章主要給大家介紹了關(guān)于pip升級(jí)pip3的快速方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12
  • 10分鐘學(xué)會(huì)使用python實(shí)現(xiàn)人臉識(shí)別(附源碼)

    10分鐘學(xué)會(huì)使用python實(shí)現(xiàn)人臉識(shí)別(附源碼)

    這篇文章主要介紹了10分鐘學(xué)會(huì)使用python實(shí)現(xiàn)人臉識(shí)別(附源碼),幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-03-03
  • Python堆棧的具體使用

    Python堆棧的具體使用

    本文主要介紹了Python堆棧的具體使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-11-11
  • Python大數(shù)據(jù)之從網(wǎng)頁(yè)上爬取數(shù)據(jù)的方法詳解

    Python大數(shù)據(jù)之從網(wǎng)頁(yè)上爬取數(shù)據(jù)的方法詳解

    這篇文章主要介紹了Python大數(shù)據(jù)之從網(wǎng)頁(yè)上爬取數(shù)據(jù)的方法,結(jié)合實(shí)例形式詳細(xì)分析了Python爬蟲爬取網(wǎng)頁(yè)數(shù)據(jù)的相關(guān)操作技巧,需要的朋友可以參考下
    2019-11-11
  • python執(zhí)行系統(tǒng)命令后獲取返回值的幾種方式集合

    python執(zhí)行系統(tǒng)命令后獲取返回值的幾種方式集合

    今天小編就為大家分享一篇python執(zhí)行系統(tǒng)命令后獲取返回值的幾種方式集合,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • Python編程語言的35個(gè)與眾不同之處(語言特征和使用技巧)

    Python編程語言的35個(gè)與眾不同之處(語言特征和使用技巧)

    這篇文章主要介紹了Python編程語言的35個(gè)與眾不同之處,Python編程語言的語言特征和使用技巧,需要的朋友可以參考下
    2014-07-07
  • 對(duì)命令行模式與python交互模式介紹

    對(duì)命令行模式與python交互模式介紹

    今天小編就為大家分享一篇對(duì)命令行模式與python交互模式介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • Python request中文亂碼問題解決方案

    Python request中文亂碼問題解決方案

    這篇文章主要介紹了Python request中文亂碼問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Python3監(jiān)控疫情的完整代碼

    Python3監(jiān)控疫情的完整代碼

    這篇文章主要介紹了Python3監(jiān)控疫情的完整代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-02-02

最新評(píng)論