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

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

 更新時間: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 會返回 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 模塊下還有另一個 uname() 函數(shù)可以使用,uname() 會返回操作系統(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é)會介紹。

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 開頭的字符串,寫在同一個條件式里。

#!/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() 會返回操作系統(tǒng)的名稱,例如:Linux、Darwin、Java、Windows 這幾種。如果無法判斷操作系統(tǒng)的話會返回空字符串。

#!/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判斷當(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 會返回當(dāng)前系統(tǒng)平臺的標(biāo)識符,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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解Python IO編程

    詳解Python IO編程

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

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

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

    wxPython的安裝與使用教程

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

    Python Scrapy?框架簡單介紹

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

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

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

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

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

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

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

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

    今天小編就為大家分享一篇tensorflow: variable的值與variable.read_value()的值區(qū)別詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    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)資料,文中通過示例代碼給大家詳細(xì)總結(jié)了兩者在語法和使用上的區(qū)別,有需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-02-02

最新評論

长汀县| 侯马市| 临夏县| 太和县| 太和县| 宾川县| 韶山市| 兰溪市| 山丹县| 电白县| 静乐县| 庄河市| 卓资县| 濮阳市| 河池市| 玉山县| 大英县| 崇仁县| 米林县| 闽清县| 五指山市| 洛南县| 神农架林区| 两当县| 连南| 方正县| 定日县| 北辰区| 淮南市| 喀喇| 杭锦旗| 兴业县| 泸水县| 榕江县| 商南县| 东安县| 九龙坡区| 犍为县| 新竹县| 汉沽区| 阳泉市|