Split path to its parts

I want to split a path into all its directory names. On Linux I would achieve this with split and a the path separator of /:

julia>             split("/home/me/some/thing", '/')
5-element Array{SubString{String},1}:
 ""     
 "home" 
 "me"   
 "some" 
 "thing"

But it would be nice if this worked everywhere. Or if these was a better way…

2 Likes

I went looking for this functionality recently too. And we’re not alone

2 Likes

See also https://github.com/JuliaLang/julia/pull/28156

2 Likes

While this is fixed in Base, a (trivial) package could be very useful. I guess the only thing it needs to do is encode platform-dependent behavior.

I had forgotten about that PR but I think we should merge it.

Oh wow, awesome.

So once/if that PR is merged, what would a package help with?

I was assuming the PR was dormant. If it is merged, that’s of course preferable.

1 Like

Merged so in Julia 1.1 you’ll be able to call splitpath. This change could potentially be added to Compat to allow use splitpath on older Julia versions.

6 Likes
?path 
help?> path
search: pathof mkpath ispath relpath abspath realpath normpath joinpath

Couldn't find path
Perhaps you meant pathof, ispath, mkpath, put!, cat, coth, hash, match or tanh
  No documentation found.

  Binding path does not exist.

not showing splitpath

splitpath certainly still exists (the output below is for Julia 1.10.4):

julia> splitpath
splitpath (generic function with 2 methods)

help?> splitpath
search: splitpath

  splitpath(path::AbstractString) -> Vector{String}

  Split a file path into all its path components. This is the opposite of joinpath. Returns an array of substrings,
  one for each directory or file in the path, including the root directory if present.

  │ Julia 1.1
  │
  │  This function requires at least Julia 1.1.

  Examples
  ≡≡≡≡≡≡≡≡

  julia> splitpath("/home/myuser/example.jl")
  4-element Vector{String}:
   "/"
   "home"
   "myuser"
   "example.jl"

The list you get from help?> path simply is not exhaustive.

1 Like