platform Module
platform module ให้ข้อมูลเกี่ยวกับ platform, OS, และ Python runtime มีประโยชน์สำหรับ cross-platform applications ที่ต้องปรับพฤติกรรมตาม OS หรือ Python version
ทำไมต้องใช้ platform
Section titled “ทำไมต้องใช้ platform”เมื่อต้องการเขียนโปรแกรมที่ทำงานได้หลาย OS platform module ช่วยตรวจสอบ:
- OS Detection - ตรวจว่า Windows, macOS, หรือ Linux
- Architecture - ตรวจว่า 32-bit หรือ 64-bit
- Python Version - ตรวจว่ารองรับ feature ใหม่หรือไม่
- Hardware Info - CPU type, machine name
- Cross-platform Paths - หา config/cache directories ที่ถูกต้อง
import platform
# ตัวอย่างการใช้งานเบื้องต้นprint("=== Quick System Info ===")print(f"OS: {platform.system()}") # Darwin, Linux, Windowsprint(f"Release: {platform.release()}") # OS versionprint(f"Machine: {platform.machine()}") # arm64, x86_64print(f"Python: {platform.python_version()}") # 3.11.0
# ใช้ตัดสินใจif platform.system() == 'Windows': print("\nRunning on Windows!")elif platform.system() == 'Darwin': print("\nRunning on macOS!")else: print(f"\nRunning on {platform.system()}")เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม
ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด
Login with Google