Welcome 歡迎光臨! 愛上網路-原本退步是向前 !

Python 判斷 OS 作業系統的 3 種方法

Python 判斷 OS 作業系統的 3 種方法 
Method 1 
Python os.name 回傳值
Linux  posix
MacOS   posix
Windows nt

Method 2
sys.platform 回傳值
┍━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━┑
│ 系統種類             │回傳值               │
┝━━━━━━━━━━━━━━━━━━━━━┿━━━━━━━━━━━━━━━━━━━━━┥
│ Linux               │ linux or linux2 (*) │
│ Windows             │ win32               │
│ Windows/Cygwin      │ cygwin              │
│ Windows/MSYS2       │ msys                │
│ Mac OS X            │ darwin              │
│ OS/2                │ os2                 │
│ OS/2 EMX            │ os2emx              │
│ RiscOS              │ riscos              │
│ AtheOS              │ atheos              │
│ FreeBSD 7           │ freebsd7            │
│ FreeBSD 8           │ freebsd8            │
│ FreeBSD N           │ freebsdN            │
│ OpenBSD 6           │ openbsd6            │
│ AIX                 │ aix (**)            │
┕━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━┙


Method 3
platform.system()回傳值
Linux  Linux
MacOS   Darwin
Windows Windows

Summary

    If you want to check if OS is Windows or Linux, or OS X, then the most reliable way is platform.system().
    If you want to make OS-specific calls, but via built-in Python modules posix or nt, then use os.name.
    If you want to get the raw OS name as supplied by the OS itself, then use sys.platform.

 

[ 程式設計 ] 瀏覽次數 : 37 更新日期 : 2025/12/29