환경에서 실행 중인 Python의 OS 및 버전에 대한 정보를 가져옵니다.

사업

표준 라이브러리 플랫폼 모듈은 Python이 실행되는 운영 체제 및 해당 버전(릴리스)에 대한 정보를 얻는 데 사용됩니다. 이 모듈을 이용하여 OS별, 버전별로 프로세스를 전환할 수 있습니다.

다음 정보가 여기에 제공됩니다.

  • OS 이름 가져오기:platform.system()
  • 버전(릴리스) 정보 얻기:platform.release(),version()
  • 한 번에 OS 및 버전 가져오기:platform.platform()
  • OS별 결과 예시
    • macOS
    • Windows
    • Ubuntu
  • OS에 따라 처리를 전환하는 샘플 코드

실행 중인 Python의 버전을 알고 싶다면 다음 문서를 참조하십시오.

전반부의 모든 샘플 코드는 macOS Mojave 10.14.2에서 실행됩니다. Windows 및 Ubuntu의 예제 결과는 후반부에 표시됩니다. OS별 기능에 대해서도 후반부에 설명합니다.

OS 이름 가져오기: platform.system()

OS 이름은 platform.system()에서 얻습니다. 반환 값은 문자열입니다.

import platform

print(platform.system())
# Darwin

버전(릴리스) 정보 얻기: platform.release(), version()

OS 버전(릴리스) 정보는 다음과 같은 기능으로 획득합니다. 두 경우 모두 반환 값은 문자열입니다.

  • 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

OS와 버전을 한번에 가져오기: platform.platform()

platform.platform()을 사용하여 OS 이름과 버전(릴리스) 정보를 함께 얻을 수 있습니다. 반환 값은 문자열입니다.

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

결과는 예제 환경에서 동일하지만 일부 운영 체제는 별칭을 OS 이름으로 반환합니다.

aliased가 true이면 시스템의 일반 이름 대신 별칭을 사용하여 결과를 반환합니다. 예를 들어 SunOS는 Solaris가 됩니다.
platform.platform() — Access to underlying platform’s identifying data — Python 3.10.0 Documentation

OS별 결과 예시

macOS, Windows 및 Ubuntu에서의 결과의 예와 OS별 기능이 표시됩니다.

맥 OS

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

macOS나 Mojave가 아니라 Darwin이라는 점에 유의하십시오.
다윈에 대한 자세한 내용은 Wikipedia 페이지를 참조하세요. macOS의 최신 버전 번호와 이름 간의 대응 관계에 대한 설명도 있습니다.

platform.mac_ver()라는 madOS 전용 함수가 있습니다.
반환 값은 튜플(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은 정수가 아니라 문자열입니다.

platform.win32_ver()라는 Windows 전용 함수가 있습니다.
반환 값은 튜플(release, version, csd, ptype)로 반환됩니다.
csd는 서비스 팩의 상태를 나타냅니다.

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

우분투

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를 사용하여 별도로 설치해야 하는 타사 라이브러리 배포판을 사용하는 것이 좋습니다.

OS에 따라 처리를 전환하는 샘플 코드

OS에 따라 사용할 함수나 메소드를 전환하고 싶다면 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