Repository navigation
pathlib strips trailing slash #65238
Copy link
Copy link
Closed as not planned
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-pathlibtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
| BPO | 21039 |
|---|---|
| Nosy | @akuchling, @pitrou, @serhiy-storchaka, @sigmavirus24, @hvenev |
| Files |
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:
assignee = None
closed_at = <Date 2015-04-14.16:54:25.933>
created_at = <Date 2014-03-23.13:55:56.401>
labels = ['type-bug', 'library']
title = 'pathlib strips trailing slash'
updated_at = <Date 2015-04-14.16:54:25.932>
user = 'https://github.com/hvenev'bugs.python.org fields:
activity = <Date 2015-04-14.16:54:25.932>
actor = 'pitrou'
assignee = 'none'
closed = True
closed_date = <Date 2015-04-14.16:54:25.933>
closer = 'pitrou'
components = ['Library (Lib)']
creation = <Date 2014-03-23.13:55:56.401>
creator = 'h.venev'
dependencies = []
files = ['34586']
hgrepos = []
issue_num = 21039
keywords = ['patch']
message_count = 15.0
messages = ['214581', '214594', '214595', '214596', '214597', '214598', '214599', '214600', '214601', '214604', '224889', '224942', '240930', '240947', '240948']
nosy_count = 7.0
nosy_names = ['akuchling', 'pitrou', 'BreamoreBoy', 'serhiy.storchaka', 'icordasc', 'h.venev', 'ischwabacher']
pr_nums = []
priority = 'normal'
resolution = 'wont fix'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue21039'
versions = ['Python 3.4', 'Python 3.5']Blocks issues
- Add pathlib.chown method #64978
- Support moving across filesystems in pathlib.Path, as shutil.move() does #73991
- Optimize
pathlib.PurePath.__fspath__()#102783 - Glob behavior is inconsistent across libraries #106747
Linked PRs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-pathlibtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Projects
- StatusShow more project fieldsDone
Activity
hvenev commentedon Mar 23, 2014
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.
pitrou commentedon 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)
pitrou commentedon Mar 23, 2014
Closing as rejected, sorry.
hvenev commentedon Mar 23, 2014
What about OpenVMS?
pitrou commentedon Mar 23, 2014
Can you elaborate? Python hasn't supported VMS for quite some time...
hvenev commentedon Mar 23, 2014
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.
pitrou commentedon Mar 23, 2014
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.
hvenev commentedon Mar 23, 2014
Or maybe URLPath?
pitrou commentedon Mar 23, 2014
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)
BreamoreBoy commentedon Mar 23, 2014
PEP-11 states that VMS was unsupported in 3.3. Code was removed from 3.4 via bpo-16136.
ischwabacher commentedon Aug 5, 2014
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:
In all other cases, the system shall prefix the remaining pathname, if any, with the contents of the symbolic link. [...]
pitrou commentedon Aug 6, 2014
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
fix: Remove os-dependent pieces of schema validation (#2357)
fix(backport): Remove os-dependent pieces of schema validation
fix(backport): Remove os-dependent pieces of schema validation (#2361)
barneygale commentedon Nov 24, 2023
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:
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.pythonGH-65238: Preserve trailing slash in pathlib
pythonGH-106747: Improve `glob()` expectations in pathlib tests
GH-106747: Improve `Path.glob()` expectations in pathlib tests (#112365)
pythonGH-65238: Add test cases for trailing slash handling in pathlib
barneygale commentedon Dec 19, 2023
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:
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/
pythonGH-106747: Improve `Path.glob()` expectations in pathlib tests (p…
pythonGH-106747: Improve `Path.glob()` expectations in pathlib tests (p…