[issue37130] pathlib.with_name() doesn't like unnamed files.
N.P. Khelili
report at bugs.python.org
Sat Jun 1 21:18:12 EDT 2019
New submission from N.P. Khelili <nophke at free.fr>:
Hi guys!
I'm new to python and working on my first real project with it....
I'm sorry if it's not the right place for posting this.
I noticed that that pathlib.with_name() method does not accept to give a name to a path that does not already have one.
It seems a bit inconsistent knowing that the Path constructor does not require one...
>>> Path()
PosixPath('.')
>>> Path().resolve()
PosixPath('/home/nono')
but:
>>> Path().with_name('dodo')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.7/pathlib.py", line 819, in with_name
raise ValueError("%r has an empty name" % (self,))
ValueError: PosixPath('.') has an empty name
whereas if you do:
>>> Path().resolve().with_name('dodo')
PosixPath('/home/dodo')
I first tought "explicit is better than implicit" and then why not allways use resolve first! That was not a big deal but then I tried:
>>> Path('..').with_name('dudu').resolve()
PosixPath('/home/nono/dudu')
( ! )
>>> Path('..').resolve().with_name('dudu')
PosixPath('/dudu')
It seems that the dots and slashes are in fact not really interpreted leading to:
>>> Path('../..').with_name('dudu')
PosixPath('../dudu')
>>> Path('../..').with_name('dudu').resolve()
PosixPath('/home/dudu')
( ! )
>>> Path('../..').resolve().with_name('dudu')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.7/pathlib.py", line 819, in with_name
raise ValueError("%r has an empty name" % (self,))
ValueError: PosixPath('/') has an empty name
Even if the doc briefly tells about this, I found this behavior quite disturbing....
I don't know what could be the correct answer to this,
maybe making Path('..') as invalid as Path('.'),
or adding a few more lines in the doc...
Sincerly yours,
----------
components: Library (Lib)
messages: 344250
nosy: Nophke
priority: normal
severity: normal
status: open
title: pathlib.with_name() doesn't like unnamed files.
type: behavior
versions: Python 3.7
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37130>
_______________________________________
More information about the Python-bugs-list
mailing list