Python中判斷當(dāng)前操作系統(tǒng)的幾種方法
本文介紹 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?requests下載文件的幾種常用方法(附代碼)
這篇文章主要介紹了五種下載方式的實(shí)現(xiàn)方法,包括基礎(chǔ)下載、大文件分塊下載、帶有斷點(diǎn)續(xù)傳的下載、帶有超時(shí)和重試的下載以及完整的下載器實(shí)現(xiàn),文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下2025-03-03
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)單程序示例分享,解釋器模式強(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ū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Python中if __name__ == "__main__"詳細(xì)解釋
這篇文章主要介紹了Python中if __name__ == "__main__"詳細(xì)解釋,需要的朋友可以參考下2014-10-10

