Bnlang File Paths

Working with file paths portably means not hard-coding / or \ and not relying on a particular working directory. The built-in path module gives you helpers that work the same way on Windows, Linux, and macOS, and accept either separator on input.


Common Operations

import "path" as path;

// Build a path from segments.
print(path.join(["data", "users", "alice.json"]));
// → "data/users/alice.json"

// Take a path apart.
print(path.dirname("/var/log/app.log"));   // → "/var/log"
print(path.basename("/var/log/app.log"));  // → "app.log"
print(path.stem("/var/log/app.log"));      // → "app"
print(path.extname("/var/log/app.log"));   // → ".log"

// Normalize a messy path.
print(path.normalize("/foo/./bar/../baz")); // → "/foo/baz"

// Is this an absolute path?
print(path.is_absolute("/etc/hosts"));      // → true
print(path.is_absolute("C:/Users/zo"));     // → true
print(path.is_absolute("./local"));         // → false

Platform-Native Separator

The helpers emit / on all platforms — that works fine for Windows filesystem APIs too. If you specifically need the platform-native separator (e.g. for shell scripts), read path.sep.

import "path" as path;
print("native separator:", path.sep);   // "/" on POSIX, "\\" on Windows

Best Practices

  • Use path.join instead of string concatenation — it handles trailing slashes and empty segments for you.
  • Use path.normalize to collapse .. and . segments after combining user-provided pieces.
  • Don't hard-code separators in string literals; let the path module pick the right form.