Although basename(path)
is designed to split file name out from path
, it doesn't check if the path is a valid file. That's to say, we can still use it to split folder name out from path
as well.
Since there're two valid usages of dir path, it's expected to get the same results:
julia> basename("~/.ssh/")
"" # should be ".ssh"
julia> basename("~/.ssh")
".ssh"
This is tested on 1.0.4
, 1.1.1
, and 1.2.0
For reference, $ basename ~/.ssh/
returns ".ssh" on the unix command line.
For reference,
$ basename ~/.ssh/
returns ".ssh" on the unix command line.
The POSIX basename(2) says to ignore trailing slashes before calculating the basename
julia's basename matches python's behavior.
Also changing this is breaking.
Personally I don't see any issues with Julia's behavior with this function. Further documentation clarifying it's behavior would also close this issue.
I think it's more like a bug because other related functions in Julia ignores the trailing "/".
Python's splitpath
:
In [4]: import os
In [5]: os.path.split("/opt/")
Out[5]: ('/opt', '')
In [6]: os.path.split("/opt")
Out[6]: ('/', 'opt')
Julia:
julia> splitpath("/opt/")
2-element Array{String,1}:
"/"
"opt"
julia> splitpath("/opt")
2-element Array{String,1}:
"/"
"opt"
The issue here is regarding the behavior basename
(not a bug since its behavior just needs better documentation). (The only issue is whether the behavior of basename
is desired or the POSIX behavior). The behavior of splitpath
that you just mentioned is a separate issue, however.
I think it's more like a bug because other related functions in Julia ignores the trailing "/".
Looks like Python's split path just splits it into a tuple of a head and tail, which is different than in Julia where it's split into a Vector of path components. So the equivalent in Julia would be:
split(p) = (dirname(p), basename(p))
In which case, we match the Python behavior.
Unrelated: I kinda question the utility of splitpath
compared to the split
definition above.
The only thing to decide here is whether the current behavior is desired and if so, document it. The current behavior is valid, but perhaps not desired in all cases, but there is nothing wrong with the answer and as mentioned Python's function behaves the same.
I don't necessarily see the precedent with Python's split
and the comparison that in general Julia ignores trailing slashes, since Python's split
and Julia's splitpath
do very different things.
Most helpful comment
The POSIX basename(2) says to ignore trailing slashes before calculating the basename