python跨平台使用吗_跨平台 - Python:我运行的操作系统是什么?

在Python中,可以通过导入os和platform模块来检测运行的操作系统。os.name可以返回'posix'(表示类Unix系统,如Linux和Mac)或'nt'(表示Windows)。platform.system()方法则能更具体地返回系统名称,如'Linux'、'Windows'或'Darwin'(Mac)。此外,还可以使用platform.release()获取操作系统版本。这些方法适用于在不同平台上区分操作系统。
摘要由CSDN通过智能技术生成

跨平台 - Python:我运行的操作系统是什么?

我需要查看我是否在Windows,Unix等上?

23个解决方案

566 votes

>>> import os

>>> print os.name

posix

>>> import platform

>>> platform.system()

'Linux'

>>> platform.release()

'2.6.22-15-generic'

Windows的输出如下:

Linux:Windows

Mac:Windows

Windows:Windows

请参阅:platform - 访问底层平台的标识数据

Louis Brandy answered 2018-12-31T04:38:53Z

155 votes

Dang - lbrandy打败了我,但这并不意味着我无法为你提供Vista的系统效果!

>>> import os

>>> os.name

'nt'

>>> import platform

>>> platform.system()

'Windows'

>>> platform.release()

'Vista'

...我无法相信没有人为Windows 10发布一个:

>>> import os

>>> os.name

'nt'

>>> import platform

>>> platform.system()

'Windows'

>>> platform.release()

'10'

Joey deVilla answered 2018-12-31T04:39:39Z

110 votes

这里的记录是Mac上的结果:

>>> import os

>>> os.name

'posix'

>>> import platform

>>> platform.system()

'Darwin'

>>> platform.release()

'8.11.1'

Mark Harrison answered 2018-12-31T04:40:13Z

77 votes

使用python区分OS的示例代码:

from sys import platform as _platform

if _platform == "linux" or _platform == "linux2":

# linux

elif _platform == "darwin":

# MAC OS X

elif _platform == "win32":

# Windows

elif _platform == "win64":

# Windows 64-bit

user39

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值