在Python中,可以使用platform
模块来获取操作系统的类型、名称、版本等信息。以下是一些常用的方法:
- 获取操作系统类型:
import platform
os_type =.system()
print(os_type) # 输出:Windows、Linux、Darwin(macOS)等
- 获取操作系统名称和版本:
import platform
os_name = platform.platform()
print(os_name) # 输出:Windows-10-10.0.19041-SP0、Linux-5.4.0-72-generic-x86_64-with-glibc2.29、Darwin-20.3.0-x86_64-i386-64bit 等
- 获取操作系统版本号:
import platform
os_version = platform.version()
print(os_version) # 输出:10.019041、5.4.0-72-generic、Darwin Kernel Version 20.3.0: Thu Jan 21 00:07:06 PST 2021; root:xnu-7195.81.3~1/RELEASE_X86_64 等
- 获取操作系统的位数:
import platform
os_bit = platform.architecture()[0]
print(os_bit) # 输出:32bit 或 64bit
这些方法可以帮助我们在编写Python程序时获取操作系统的相关信息,便更好地适配不同的操作系统。