我需要看些什么来确定我使用的是Windows还是Unix等等?
当前回答
您可以查看pip-date包的一部分pyOSinfo中的代码,以获得最相关的操作系统信息,就像您的Python发行版所看到的那样。
人们检查操作系统最常见的原因之一是终端兼容性,以及某些系统命令是否可用。不幸的是,这种检查是否成功在一定程度上取决于你的python安装和操作系统。例如,uname在大多数Windows python包中是不可用的。上面的python程序将向您展示最常用的内置函数的输出,这些函数已经由os, sys, platform, site提供。
因此,只获得基本代码的最好方法是将其视为示例。(我想我可以直接粘贴在这里,但这样做就不政治正确了。)
其他回答
在windows 8上的有趣结果:
>>> import os
>>> os.name
'nt'
>>> import platform
>>> platform.system()
'Windows'
>>> platform.release()
'post2008Server'
编辑:这是一个bug
试试这个:
import os
os.uname()
你可以这样做:
info=os.uname()
info[0]
info[1]
同样的,....
import platform
is_windows=(platform.system().lower().find("win") > -1)
if(is_windows): lv_dll=LV_dll("my_so_dll.dll")
else: lv_dll=LV_dll("./my_so_dll.so")
我知道这是一个老问题,但我相信我的答案可能会对那些正在寻找一种简单易懂的python方法来检测代码中的操作系统的人有所帮助。在python3.7上测试
from sys import platform
class UnsupportedPlatform(Exception):
pass
if "linux" in platform:
print("linux")
elif "darwin" in platform:
print("mac")
elif "win" in platform:
print("windows")
else:
raise UnsupportedPlatform
这个解决方案适用于python和jython。
模块os_identify.py:
import platform
import os
# This module contains functions to determine the basic type of
# OS we are running on.
# Contrary to the functions in the `os` and `platform` modules,
# these allow to identify the actual basic OS,
# no matter whether running on the `python` or `jython` interpreter.
def is_linux():
try:
platform.linux_distribution()
return True
except:
return False
def is_windows():
try:
platform.win32_ver()
return True
except:
return False
def is_mac():
try:
platform.mac_ver()
return True
except:
return False
def name():
if is_linux():
return "Linux"
elif is_windows():
return "Windows"
elif is_mac():
return "Mac"
else:
return "<unknown>"
像这样使用:
import os_identify
print "My OS: " + os_identify.name()
推荐文章
- 为什么我得到“'str'对象没有属性'读取'”当尝试使用' json。载入字符串?
- 不区分大小写的列表排序,没有降低结果?
- 排序后的语法(key=lambda:…)
- 在烧瓶中返回HTTP状态代码201
- 使用python创建一个简单的XML文件
- APT命令行界面式的yes/no输入?
- 如何打印出状态栏和百分比?
- 在Python中获取大文件的MD5哈希值
- 在Python格式字符串中%s是什么意思?
- 如何循环通过所有但最后一项的列表?
- python用什么方法避免默认参数为空列表?
- 如何在Java中监控计算机的CPU、内存和磁盘使用情况?
- ValueError: numpy。Ndarray大小改变,可能表示二进制不兼容。期望从C头得到88,从PyObject得到80
- Anaconda /conda -安装特定的软件包版本
- 我在哪里调用Keras的BatchNormalization函数?