0
点赞
收藏
分享

微信扫一扫

【python初级】os.name获得当前操作系统的类型

艾晓雪 2022-03-11 阅读 75

【python初级】os.name获得当前操作系统的类型

1、背景

我们写的脚本可能会考虑在windows以及linux的不同。
即允许根据代码运行的操作系统运行不同的代码。

python模块的os.name返回当前操作系统的类型,
可能会返回posix , nt , java, 分别对应linux/windows/java虚拟机!

2、示例

在windows下:

C:\Users\Administrator>python
Python 3.8.8 (tags/v3.8.8:024d805, Feb 19 2021, 13:18:16) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.name
'nt'
>>>

在linux下:

(base) root@iZuf63978tmw06cpq8okr2Z:~# python
Python 3.7.6 (default, Jan  8 2020, 19:59:22) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.name
'posix'
>>> 
举报

相关推荐

0 条评论