Python判断操作系统类型
本文作者为吕浪(Victor Lv),转载请注明出处。
python(2.7) check the os type running on:
1. use sys.platform:
windows:
>>> import sys
>>> sys.platform
>>> 'win32'
linux
>>> import sys
>>> sys.platform
>>> 'linux2'
2. use platform.system():
windows:
>>> import platform
>>> platform.system()
>>> 'Windows'
linux:
>>> import platform
>>> platform.system()
>>> 'Linux'
3. Judge the os type:
We can use "=="(equal) to judge whether the os is window or linux, or we can use string.startwith() function to check it:
if sys.platform.startswith('freebsd'):
# FreeBSD-specific code here...
elif sys.platform.startswith('linux'):
# Linux-specific code here...
Reference:
platform — Access to underlying platform’s identifying data