如何确定运行 Python 脚本的操作系统是否类 Unix? [英] How can I determine if the operating system a Python script is running on is Unix-like?

查看:24
本文介绍了如何确定运行 Python 脚本的操作系统是否类 Unix?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 Python 脚本确定操作系统是否基于 Unix.我可以想到两种方法来做到这一点,但它们都有缺点:

I'm trying to determine if the operating system is Unix-based from a Python script. I can think of two ways to do this but both of them have disadvantages:

  • 检查 platform.system() 是否在一个元组中,例如 ("Linux", "Darwin").这样做的问题是我不想提供每个制造的每个类 Unix 系统的列表,尤其是有许多 *BSD 变体.
  • 检查函数 os.fchmod 是否存在,因为该函数仅在 Unix 上可用.这似乎不是一种干净的或Pythonic"的方式来做到这一点.
  • Check if platform.system() is in a tuple such as ("Linux", "Darwin"). The problem with this is that I don't want to provide a list of every Unix-like system every made, in particular there are many *BSD varieties.
  • Check if the function os.fchmod exists, as this function is only available on Unix. This doesn't seem like a clean or "Pythonic" way to do it.

推荐答案

Pythonic 的做法是不关心你在什么平台上.

The Pythonic way to do it is not to care what platform you are on.

如果根据平台有多种不同的设施来完成某事,那么将它们抽象到一个函数或类之后,如果该设施在当前平台上不可用,则应该尝试一个设施并转移到另一个设施.

If there are multiple different facilities to accomplish something depending on the platform, then abstract them behind a function or class, which should try a facility and move on to another if that facility is not available on the current platform.

这篇关于如何确定运行 Python 脚本的操作系统是否类 Unix?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆