Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion base/path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,26 @@ julia> dirname("/home/myuser/")

See also: [`basename`](@ref)
"""
dirname(path::AbstractString) = splitdir(path)[1]
dirname(path::AbstractString) = splitdir(path)[1]

"""
dirname(path::AbstractString, n::Integer) -> AbstractString
Get the `n`-th parent directory of a path. Trailing characters ('/' or '\\') in the path
are counted as part of the path.

# Examples
```jldoctest
julia> dirname("/home/myuser", 1)
"/home"

julia> dirname("/home/myuser/", 1)
"/home/myuser"

julia> dirname("/home/myuser/stats/plots", 3)
"/home"
```
"""
dirname(path::AbstractString, n::Integer) = ∘(repeat([dirname], n)...)(path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just n <= 0 ? path : dirname(dirname(path), n-1)


"""
basename(path::AbstractString) -> AbstractString
Expand Down
2 changes: 2 additions & 0 deletions test/path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
end
@test basename(S("foo$(sep)bar")) == "bar"
@test dirname(S("foo$(sep)bar")) == "foo"
@test dirname(S("foo$(sep)bar"), 1) == "foo"
@test dirname(S("a$(sep)b$(sep)c$(sep)d"), 3) == "a"

@testset "expanduser" begin
@test expanduser(S("")) == ""
Expand Down