Repository navigation
pathlib strips trailing slash #65238
Description
Activity
Some programs' behavior is different depending on whether the path has a trailing slash or not. Examples include ls, cp, mv, ln, rm and rsync. URL paths may also behave differently. For example http://xkcd.com/1 redirects to http://xkcd.com/1/
Boost.Filesystem's path class also supports trailing slashes in paths. C++'s filesystem library proposal is also based on Boost.Filesystem.
- addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Mar 23, 2014 Yes, this is by design. The occasional difference between slash-ended and non-slash-ended paths is unexpected and potentially confusing. Moreover, it's not a property of the OS itself - it's just some syntactic sugar to enable an option such as resolving symlinks. pathlib paths represent filesystem paths, not arbitrary shell arguments.
Similarly, pathlib doesn't have special processing for "~someuser" parts.
(as for URL paths, they are not part of the design space of pathlib)
Closing as rejected, sorry.
What about OpenVMS?
Can you elaborate? Python hasn't supported VMS for quite some time...
AFAIK paths on OpenVMS are represented in a strange way. [dir.subdir]filename is a path for a file and [dir.subdir.anothersubdir] is a path for a directory.
Then I'm afraid the current Path classes won't do a good job of representing them :-)
But as I said, Python probably doesn't run on VMS anymore, so this is a rather theoretical problem. Maybe if some day Python supports VMS again, someone can contribute a VMSPath implementation.
Or maybe URLPath?
Or maybe URLPath?
I'm skeptical about that. I think someone should first prototype a
PureURLPath and maybe publish it on PyPI.
(as for the non-pure variant, URLPath, it doesn't seem to make sense)This may be only syntactic sugar, but it is POSIX-specified syntactic sugar: according to http://pubs.opengroup.org/onlinepubs/9699919799/. trailing slashes in pathnames are semantically meaningful in pathname resolution. Tilde escapes are not mentioned.
4.12 Pathname Resolution
========================[...]
A pathname that contains at least one non- <slash> character and that ends with one or more trailing <slash> characters shall not be resolved successfully unless the last pathname component before the trailing <slash> characters names an existing directory or a directory entry that is to be created for a directory immediately after the pathname is resolved. Interfaces using pathname resolution may specify additional constraints[1] when a pathname that does not name an existing directory contains at least one non- <slash> character and contains one or more trailing <slash> characters.
If a symbolic link is encountered during pathname resolution, the behavior shall depend on whether the pathname component is at the end of the pathname and on the function being performed. If all of the following are true, then pathname resolution is complete:
1. This is the last pathname component of the pathname. 2. The pathname has no trailing <slash>. 3. The function is required to act on the symbolic link itself, or certain arguments direct that the function act on the symbolic link itself.In all other cases, the system shall prefix the remaining pathname, if any, with the contents of the symbolic link. [...]
Isaac, thanks for the reference. I'm reopening the issue for discussion (although I'm still not convinced this would be actually a good thing).
May I ask you to post on the python-dev mailing-list for further feedback?13 remaining items
So maybe an alternative approach could be not to have the behavior be indicated by some property of the
Pathinstance but by using different attributes. So maybe e.g.Path("foo/").namewould return"foo"butPath("foo/").alt_namewould return"". This would avoid the scenario I described just above.Now we just have to decide on names for the attributes that could have this alternate behavior (are there others besides
.parentand.name?). And probably the implementation will have to keep track of the trailing slash somehow.I've been working on a patch along these lines. The idea is to ignore any trailing slash whenever we split a path into (dirname, basename). So:
>>> from pathlib import PurePosixPath >>> p = PurePosixPath('/home/barney/') >>> p.parent PurePosixPath('/home') >>> list(p.parents) [PurePosixPath('/home'), PurePosixPath('/')] >>> p.name 'barney' >>> p.with_name('fred') PurePosixPath('/home/fred/')
This also applies to
[with_]stem,[with_]suffix,suffixes,[is_]relative_to.Users wouldn't be able to call
path.parentto remove a trailing slash, so we'd need to add something likePurePath.[with_]trailerI think.Re-resolving as "won't fix" - the present behaviour is desirable for many users and it's too dangerous to change now. The crux of the issue is:
>>> str(PurePath('a/')) == 'a' True >>> PurePath('a/') == PurePath('a') True
Fixing this bug entails changing the first result, which entails changing the second too. For some this is clearly a bugfix, but for many others it comes close to defeating the purpose of pathlib.
I don't think an initialiser argument would help: users handed a
Pathobject would still need to cope with the possible equality and string representation change.Much more discussion here: https://discuss.python.org/t/pathlib-preserve-trailing-slash/33389/
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Blocks issues
pathlib.PurePath.__fspath__()#102783Linked PRs