Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consistent result of basename for dir path #33000

Closed
johnnychen94 opened this issue Aug 21, 2019 · 7 comments · Fixed by #37580
Closed

consistent result of basename for dir path #33000

johnnychen94 opened this issue Aug 21, 2019 · 7 comments · Fixed by #37580

Comments

@johnnychen94
Copy link
Sponsor Member

johnnychen94 commented Aug 21, 2019

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

@rfourquet
Copy link
Member

For reference, $ basename ~/.ssh/ returns ".ssh" on the unix command line.

@sjoelund
Copy link

For reference, $ basename ~/.ssh/ returns ".ssh" on the unix command line.

The POSIX basename(2) says to ignore trailing slashes before calculating the basename

@musm
Copy link
Contributor

musm commented Aug 22, 2019

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.

@johnnychen94
Copy link
Sponsor Member Author

johnnychen94 commented Aug 22, 2019

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"

@musm
Copy link
Contributor

musm commented Aug 22, 2019

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.

@musm
Copy link
Contributor

musm commented Sep 15, 2020

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.

@musm
Copy link
Contributor

musm commented Sep 15, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants