獲取有關在環境中運行的操作系統和 Python 版本的信息。

商業

標準庫平台模塊用於獲取有關運行 Python 的操作系統及其版本(發行版)的信息。使用這個模塊,可以切換每個操作系統和版本的進程。

此處提供以下信息。

  • 獲取操作系統名稱:platform.system()
  • 獲取版本(發布)信息:platform.release(),version()
  • 立即獲取操作系統和版本:platform.platform()
  • 每個操作系統的結果示例
    • macOS
    • Windows
    • Ubuntu
  • 根據操作系統切換處理的示例代碼

如果您想知道您正在運行的 Python 版本,請參閱以下文章。

上半部分的所有示例代碼都在 macOS Mojave 10.14.2 上運行; Windows 和 Ubuntu 上的示例結果顯示在下半部分;操作系統特定的功能也在後半部分討論。

獲取操作系統名稱:platform.system()

操作系統名稱由 platform.system() 獲得。返回值是一個字符串。

import platform

print(platform.system())
# Darwin

獲取版本(發布)信息:platform.release(), version()

使用以下函數獲取操作系統版本(發布)信息。在這兩種情況下,返回值都是一個字符串。

  • platform.release()
  • platform.version()

如以下示例所示,platform.release() 返回更簡單的內容。

print(platform.release())
# 18.2.0

print(platform.version())
# Darwin Kernel Version 18.2.0: Mon Nov 12 20:24:46 PST 2018; root:xnu-4903.231.4~2/RELEASE_X86_64

一次獲取操作系統和版本:platform.platform()

可以使用 platform.platform() 一起獲取操作系統名稱和版本(發布)信息。返回值是一個字符串。

print(platform.platform())
# Darwin-18.2.0-x86_64-i386-64bit

如果參數 terse 的值為 TRUE,則只會返回最少的信息。

print(platform.platform(terse=True))
# Darwin-18.2.0

還有一個參數別名。

print(platform.platform(aliased=True))
# Darwin-18.2.0-x86_64-i386-64bit

示例環境中的結果相同,但某些操作系統會返回別名作為操作系統名稱。

如果 aliased 為 true,則使用別名而不是系統的通用名稱返回結果。例如,SunOS 變成了 Solaris。
platform.platform() — Access to underlying platform’s identifying data — Python 3.10.0 Documentation

每個操作系統的結果示例

將顯示 macOS、Windows 和 Ubuntu 上的結果示例,以及特定於操作系統的功能。

蘋果系統

macOS Mojave 10.14.2 上的結果示例。與上面顯示的示例相同。

print(platform.system())
# Darwin

print(platform.release())
# 18.2.0

print(platform.version())
# Darwin Kernel Version 18.2.0: Mon Nov 12 20:24:46 PST 2018; root:xnu-4903.231.4~2/RELEASE_X86_64

print(platform.platform())
# Darwin-18.2.0-x86_64-i386-64bit

請注意,它是 Darwin,而不是 macOS 或 Mojave。
有關達爾文的更多信息,請參閱維基百科頁面。還有macOS中最新版本號和名稱對應關係的說明。

有一個特定於 madOS 的函數稱為 platform.mac_ver()。
返回值作為元組返回(release、versioninfo、machine)。
在示例環境中,versioninfo 是未知的,是一個空字符串元組。

print(platform.mac_ver())
# ('10.14.2', ('', '', ''), 'x86_64')

視窗

Windows 10 Home 上的結果示例。

print(platform.system())
# Windows

print(platform.release())
# 10

print(platform.version())
# 10.0.17763

print(platform.platform())
# Windows-10-10.0.17763-SP0

請注意,platform.release() 的返回值 10 是一個字符串,而不是一個整數。

有一個特定於 Windows 的函數稱為 platform.win32_ver()。
返回值作為元組返回(release、version、csd、ptype)。
csd 指示服務包的狀態。

print(platform.win32_ver())
# ('10', '10.0.17763', 'SP0', 'Multiprocessor Free')

Ubuntu

Ubuntu 18.04.1 LTS 上的結果示例。

print(platform.system())
# Linux

print(platform.release())
# 4.15.0-42-generic

print(platform.version())
# #45-Ubuntu SMP Thu Nov 15 19:32:57 UTC 2018

print(platform.platform())
# Linux-4.15.0-44-generic-x86_64-with-Ubuntu-18.04-bionic

有一個 Unix 特定的函數 platform.linux_distribution()。
返回值作為元組返回(distname、version、id)。

print(platform.linux_distribution())
# ('Ubuntu', '18.04', 'bionic')

請注意,platform.linux_distribution() 已在 Python 3.8 中刪除。建議改用第三方庫發行版,需要單獨使用pip安裝。

根據操作系統切換處理的示例代碼

如果要根據操作系統切換要使用的函數或方法,可以使用諸如 platform.system() 之類的方法來確定值。

以下是獲取文件創建日期的示例。

def creation_date(path_to_file):
    """
    Try to get the date that a file was created, falling back to when it was
    last modified if that isn't possible.
    See http://stackoverflow.com/a/39501288/1709587 for explanation.
    """
    if platform.system() == 'Windows':
        return os.path.getctime(path_to_file)
    else:
        stat = os.stat(path_to_file)
        try:
            return stat.st_birthtime
        except AttributeError:
            # We're probably on Linux. No easy way to get creation dates here,
            # so we'll settle for when its content was last modified.
            return stat.st_mtime

在這個例子中,platform.system() 的值首先被用來確定它是 Windows 還是其他。
然後,它進一步使用異常處理在存在 st_birthtime 屬性的情況和其他情況之間切換過程。

Copied title and URL