Sujet : Re: pathlib.Path.is_file vs os.path.isfile difference
De : grant.b.edwards (at) *nospam* gmail.com (Grant Edwards)
Groupes : comp.lang.pythonDate : 08. Mar 2024, 21:30:51
Autres entêtes
Message-ID : <mailman.69.1709926253.3452.python-list@python.org>
References : 1 2 3
User-Agent : slrn/1.0.3 (Linux)
On 2024-03-08, Grant Edwards via Python-list <
python-list@python.org> wrote:
On 2024-03-08, Thomas Passin via Python-list <python-list@python.org> wrote:
On 3/8/2024 1:03 PM, Albert-Jan Roskam via Python-list wrote:
Hi,
I was replacing some os.path stuff with Pathlib and I discovered this:
Path(256 * "x").is_file() # OSError
os.path.isfile(256 * "x") # bool
Is this intended? Does pathlib try to resemble os.path as closely as
possible?
>
You must have an very old version of Python. I'm running 3.12.2 and it returns False.
>
It throws OSError with Python 3.11.8 on Linux.
OSError: [Errno 36] File name too long: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
On all of the Linux filesystems I know about, the max length for a
filename is 255 bytes, so the OSError is too surprising, and it does
seem to follow the documentation.
import os
os.path.isfile(256 * "x")
False
However, os.path.isfile() apprently masks that error somehow and
returns False instead.
I notice that the os.path.isfile() documentation does not specify what
happens if the path is not a file or is illegal. It only specifies
that True is returned if the path is a regular file. Presumably
something other than "return True" is supposed to happen, but exactly
what is not specified.