platform库
在Python中,你可以使用platform
模块来确定你正在使用的操作系统类型。以下是一个示例代码,用于判断当前系统是Windows还是Linux:
import platform
if platform.system() == 'Windows':
print('This is Windows')
elif platform.system() == 'Linux':
print('This is Linux')
else:
print('Unknown OS')
这段代码首先导入了platform
模块,然后使用platform.system()
函数获取操作系统的名称。然后根据返回的名称来判断操作系统类型。
封装函数
utils.py
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import platform
def is_system():
"""
Windows:1
Linux :2
Unknown:0
"""
if platform.system() == 'Windows':
return 1
elif platform.system() == 'Linux':
return 2
else:
return 0
if __name__ == '__main__':
if is_system() == 1:
print("调用windows依赖")
elif is_system() == 2:
print("调用linux依赖")
else:
print("操作系统错误.")
sys.exit(-1)