Cross-platform development keeps software runnable across operating systems or hardware platforms without rewriting platform-specific logic. Python supports this goal by running on Windows, Linux, and macOS. section 1.2
File paths show why portability depends on implementation choices. Windows and macOS use different separators, so constructing paths with pathlib avoids embedding a platform-specific string such as data\\subject_123.dat. section 11.4.1
Apply cross-platform practices when working with files:
- Represent locations with Path objects and combine directory_path / filename instead of writing separators manually. section 11.4.1
- Keep project files readable and organized so portability does not come at the expense of maintainability. section 14.5
- Prefer platform-agnostic web applications when a project needs to be showcased across environments. section 14.5
Generating portable data files
Create a data directory, build each filename with an f-string, and combine it with the directory using filepath = data_folder / filename. The same pattern works across Windows and macOS, while open writes the generated files. section 11.4.1
subject_ids = [123, 124, 125]
extensions = ["config", "dat", "txt"]
for subject_id in subject_ids:
for extension in extensions:
filename = f"subject_{subject_id}.{extension}"
filepath = data_folder / filename #1
with open(filepath, "w") as file:
file.write(f"It's the file {filename}.")
Inspect the complete listing for creating several subject files with portable pathlib paths. section 11.4.1

