最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2025年05月14日 09:19:12   作者:知來(lái)者逆  
這篇文章主要為大家詳細(xì)介紹了Python中判斷當(dāng)前操作系統(tǒng)的三種方法,主要是os.name,sys.platform和platform.system(),下面小編就來(lái)和大家詳細(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 下沒(méi)有 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 開(kāi)頭的字符串,寫(xiě)在同一個(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 這幾種。如果無(wú)法判斷操作系統(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ù)來(lái)進(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 IO編程

    詳解Python IO編程

    這篇文章主要介紹了Python IO編程的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • Python?requests下載文件的幾種常用方法(附代碼)

    Python?requests下載文件的幾種常用方法(附代碼)

    這篇文章主要介紹了五種下載方式的實(shí)現(xiàn)方法,包括基礎(chǔ)下載、大文件分塊下載、帶有斷點(diǎn)續(xù)傳的下載、帶有超時(shí)和重試的下載以及完整的下載器實(shí)現(xiàn),文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下
    2025-03-03
  • wxPython的安裝與使用教程

    wxPython的安裝與使用教程

    wxPython是Python語(yǔ)言的一套優(yōu)秀的GUI圖形庫(kù)。wxPython可以很方便的創(chuàng)建完整的、功能鍵全的GUI用戶界面。這篇文章給大家介紹了wxPython的安裝與使用,感興趣的朋友一起看看吧
    2018-08-08
  • Python Scrapy?框架簡(jiǎn)單介紹

    Python Scrapy?框架簡(jiǎn)單介紹

    Scrapy是適用于Python的一個(gè)快速、高層次的屏幕抓取和web抓取框架,用于抓取web站點(diǎn)并從頁(yè)面中提取結(jié)構(gòu)化的數(shù)據(jù),這篇文章主要介紹了Scrapy框架優(yōu)點(diǎn)及簡(jiǎn)單介紹,需要的朋友可以參考下
    2023-05-05
  • Pytorch 圖像變換函數(shù)集合小結(jié)

    Pytorch 圖像變換函數(shù)集合小結(jié)

    這篇文章主要介紹了Pytorch 圖像變換函數(shù)集合小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • Python標(biāo)準(zhǔn)庫(kù)之Math,Random模塊使用詳解

    Python標(biāo)準(zhǔn)庫(kù)之Math,Random模塊使用詳解

    math數(shù)學(xué)模塊和random隨機(jī)模塊是Python常用的標(biāo)準(zhǔn)庫(kù)之一。本文將詳細(xì)為大家介紹一下這兩個(gè)模塊的使用方法,需要的小伙伴可以參考一下
    2022-05-05
  • Python設(shè)計(jì)模式編程中解釋器模式的簡(jiǎn)單程序示例分享

    Python設(shè)計(jì)模式編程中解釋器模式的簡(jiǎn)單程序示例分享

    這篇文章主要介紹了Python設(shè)計(jì)模式編程中解釋器模式的簡(jiǎn)單程序示例分享,解釋器模式強(qiáng)調(diào)用抽象類來(lái)表達(dá)程序中將要實(shí)現(xiàn)的功能,需要的朋友可以參考下
    2016-03-03
  • tensorflow: variable的值與variable.read_value()的值區(qū)別詳解

    tensorflow: variable的值與variable.read_value()的值區(qū)別詳解

    今天小編就為大家分享一篇tensorflow: variable的值與variable.read_value()的值區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • Python中if __name__ ==

    Python中if __name__ == "__main__"詳細(xì)解釋

    這篇文章主要介紹了Python中if __name__ == "__main__"詳細(xì)解釋,需要的朋友可以參考下
    2014-10-10
  • 詳解Python中的靜態(tài)方法與類成員方法

    詳解Python中的靜態(tài)方法與類成員方法

    這篇文章主要介紹了關(guān)于Python中靜態(tài)方法與類成員的相關(guān)資料,文中通過(guò)示例代碼給大家詳細(xì)總結(jié)了兩者在語(yǔ)法和使用上的區(qū)別,有需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-02-02

最新評(píng)論

吉林省| 新疆| 文安县| 玛沁县| 财经| 酒泉市| 马公市| 龙州县| 京山县| 武川县| 肥乡县| 寻乌县| 多伦县| 海门市| 金川县| 兴隆县| 黄石市| 永城市| 昌平区| 遂宁市| 南宁市| 商洛市| 定结县| 天等县| 南开区| 广昌县| 大悟县| 锦州市| 本溪| 招远市| 清徐县| 炎陵县| 台中市| 青浦区| 浦北县| 通化市| 洛浦县| 南康市| 金门县| 沙湾县| 镇雄县|