Code of the Day
AdvancedCross-Platform

Lab: cross-platform

Apply path portability, platform-specific branching, and script execution decisions.

Lab · optionalUtilitiesAdvanced9 min
Recommended first
By the end of this lesson you will be able to:
  • Construct paths portably using pathlib instead of string concatenation
  • Choose the right platform detection strategy for a situation
  • Write a shebang that works across typical Unix installations
  • Diagnose a portability failure from its symptoms

Optional scenario lab. Cross-platform issues are often subtle — the same code behaves differently on macOS, Linux, and Windows in ways that don't appear until a user files a bug. Practice the decisions here.

Scenarios: cross-platform utilities

  1. 1.
    You need to join a base directory path with a filename in Python. Most portable approach?
  2. 2.
    Your CLI needs to open a browser. The command is open on macOS, xdg-open on Linux, and start on Windows. Best approach?
  3. 3.
    You want a Python script to be executable with ./myscript.py on any Unix-like system. Which shebang is most portable?
  4. 4.
    A Python module named Utils.py can be imported as import utils on macOS but fails on Linux. The root cause is filesystem case sensitivity.
  5. 5.
    Your CLI stores user configuration. Where should the config file live for proper cross-platform behavior?

The cross-platform mindset: use the platform's abstractions (pathlib, webbrowser, platformdirs) rather than hard-coding OS assumptions. Test on at least two platforms before calling something portable.

Finished reading? Mark it complete to track your progress.