-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
pathlib .suffix, .suffixes, .stem unexpected behavior for pathname with trailing dot #82805
Comments
|
Python3.8 pathlib treats dot between path stem and suffix as part of suffix in general: >>> a = pathlib.Path('foo.txt')
>>> a.stem, a.suffix
('foo', '.txt')
>>> a.with_suffix('')
PosixPath('foo')However, if pathname ends with dot, it treats the trailing dot as part of stem, not part of suffix: >>> b = pathlib.Path('bar.')
>>> b.stem, b.suffix
('bar.', '')This looks like a bug. It should return ('bar', '.'). >>> pathlib.Path('foo.txt').with_suffix('.')
...
ValueError: Invalid suffix '.' <== Why not PosixPath('foo.') ?
>>> c = pathlib.Path('foo..')
>>> c.stem, c.suffix, c.suffixes
('foo..', '', [])I think above should return ('foo.', '.', ['.', '.']) Tested with macOS 10.15 and Python3.8. Python3.7 behaves the same. |
|
In my attempt to work on this bug, I found it is not possible to make a fix without deciding whether files with trailing dots meaningfully contain a suffix. Currently, Python implicitly defines strings with trailing dots to be an invalid suffix, but does not error: # Yes, obvious case
>>> Path("foo.tar").suffix
'.tar'
# Trailing dot is not a suffix
>>> Path("foo.tar.").suffix
''
# Consistent with before, but confusingly you can create one with `with_suffix()`
>>> Path("foo").with_suffix(".tar.")
PosixPath('foo.tar.')
>>> PosixPath('foo.tar.').with_suffix(".tar.").suffix
''So, this isn't a bug if "trailing dots is not a valid suffix". However, this rule is seemingly ignored in My proposed solution (outside the scope of this issue) is to change |
|
I agree with the original bug report - |
|
@barneygale Good point, then it must be that >>> Path("foo.tar.").suffixes
[".tar", "."]As well as we needing to remove the restriction with |
|
More discussion in #100157, which I've closed as a duplicate. |
pathlib now treats "`.`" as a valid file extension (suffix). This brings it in line with `os.path.splitext()`. In the (private) pathlib ABCs, we add a new `ParserBase.splitext()` method that splits a path into a `(root, ext)` pair, like `os.path.splitext()`. This method is called by `PurePathBase.stem`, `suffix`, etc. In a future version of pathlib, we might make these base classes public, and so users will be able to define their own `splitext()` method to control file extension splitting. In `pathlib.PurePath` we add optimised `stem`, `suffix` and `suffixes` properties that don't use `splitext()`, which avoids computing the path base name twice.
pathlib now treats "`.`" as a valid file extension (suffix). This brings it in line with `os.path.splitext()`. In the (private) pathlib ABCs, we add a new `ParserBase.splitext()` method that splits a path into a `(root, ext)` pair, like `os.path.splitext()`. This method is called by `PurePathBase.stem`, `suffix`, etc. In a future version of pathlib, we might make these base classes public, and so users will be able to define their own `splitext()` method to control file extension splitting. In `pathlib.PurePath` we add optimised `stem`, `suffix` and `suffixes` properties that don't use `splitext()`, which avoids computing the path base name twice.
|
PR available: #118952 |
) pathlib now treats "`.`" as a valid file extension (suffix). This brings it in line with `os.path.splitext()`. In the (private) pathlib ABCs, we add a new `ParserBase.splitext()` method that splits a path into a `(root, ext)` pair, like `os.path.splitext()`. This method is called by `PurePathBase.stem`, `suffix`, etc. In a future version of pathlib, we might make these base classes public, and so users will be able to define their own `splitext()` method to control file extension splitting. In `pathlib.PurePath` we add optimised `stem`, `suffix` and `suffixes` properties that don't use `splitext()`, which avoids computing the path base name twice.
…python#118952) pathlib now treats "`.`" as a valid file extension (suffix). This brings it in line with `os.path.splitext()`. In the (private) pathlib ABCs, we add a new `ParserBase.splitext()` method that splits a path into a `(root, ext)` pair, like `os.path.splitext()`. This method is called by `PurePathBase.stem`, `suffix`, etc. In a future version of pathlib, we might make these base classes public, and so users will be able to define their own `splitext()` method to control file extension splitting. In `pathlib.PurePath` we add optimised `stem`, `suffix` and `suffixes` properties that don't use `splitext()`, which avoids computing the path base name twice.
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:
Linked PRs
The text was updated successfully, but these errors were encountered: