Platform Detection & Portable Code
`sys.platform` and the `platform` module allow you to detect host operating system architectures (Linux, macOS, Windows) to write cross-platform portable software 🖥️.
import sys, platform
print(f"OS Identifier: {sys.platform} 💻")
print(f"Architecture: {platform.machine()} ⚙️")
if sys.platform == "win32":
print("Running on Windows")
elif sys.platform == "darwin":
print("Running on macOS")
else:
print("Running on Linux / Unix 🐧")
# 109. Platform Detection & Portable Code `sys.platform` and the `platform` module allow you to detect host operating system architectures (Linux, macOS, Windows) to write cross-platform portable software 🖥️. ```python import sys, platform print(f"OS Identifier: {sys.platform} 💻") print(f"Architecture: {platform.machine()} ⚙️") if sys.platform == "win32": print("Running on Windows") elif sys.platform == "darwin": print("Running on macOS") else: print("Running on Linux / Unix 🐧") ``` Always check `sys.platform.startswith('linux')` when handling Linux-specific syscalls like `epoll` or `/proc` file queries! 🖥️ --- ### Question What string does `sys.platform` return on Apple macOS? 🧠 - [ ] a. `'darwin'` 🍏 - [ ] b. `'macos'` - [ ] c. `'osx'` - [ ] d. `'apple'` --- ### Challenge Import `sys`. Write a function `is_unix()` that returns `sys.platform in ('linux', 'darwin')`. Print `is_unix()`. 🎯 <details> <summary>Solution</summary> ```python import sys def is_unix(): return sys.platform in ("linux", "darwin") print(is_unix()) ``` </details>