Is there a way to split a path "a/b/c.txt" into [ "a", "B", "c.txt" ]?
I could call Base.Filesystem.splitdir() in a loop until the first element is the empty string, but that seems wasteful. I could call split( "a/b/c.txt", Base.Filesystem.path_separator ), but Base.Filesystem.path_separator is undocumented.
In practice, I want to check if the first directory in the path is “…”.
"""
splitpath( path::String ) -> Array{String}
Splits a path into an array of its path components. The inverse of `joinpath()`.
Calling `joinpath( splitpath( path )... )` should produce `path`
(possible with the trailing slash removed).
```jldoctest
julia> splitpath("a/b/c")
("a", "b", "c")
julia> splitpath("/a/b/c")
("/", "a", "b", "c")
```
"""
function splitpath( path::String )
result = String[]
while path != ""
path, last = splitdir( path )
## If path consists of only the path separator, which could happen
## when referring to the filesystem root, then last will be empty
## and path will be unchanged.
## If this is the case, swap them so that we push the root path
## marker and then the while loop will terminate.
if last == ""
path, last = last, path
end
push!( result, last )
end
reverse!( result )
return result
end
The computationally efficient way to write it is split( "a/b/c.txt", Base.Filesystem.path_separator ). However, Base.Filesystem.path_separator is not documented. In contrast, Python exposes os.sep.