How can I check if a filesystem path is absolute or relative in a cross-platform way?
For my purposes, I consider a path absolute if points to a location that is independent of
I only need to check paths valid on the current operating system. I never need to check Windows paths on Unix or vice versa.
Examples of absolute paths on Unix:
~/baz(This is sometimes considered relative as it is user
$UserName-dependent. However, it is not
Relative paths on Unix:
Relative paths on Windows:
\foo\bar(May be either of
- any of the above with a
/separator instead of
These are just a few examples. I may have missed several special cases. I am looking for something which always works and preferably doesn't depend on my own partial understanding of what forms an absolute or relative path may take on different OSs. I would rather not start matching strings, at least not without a reference document which gives an exhaustive list of all possible path syntaxes both for absolute and relative paths.
The ideal solution would use a feature that asks the operating system directly: