pathlib.Path.is_file vs os.path.isfile difference

Grant Edwards grant.b.edwards at gmail.com
Fri Mar 8 14:21:39 EST 2024


On 2024-03-08, Thomas Passin via Python-list <python-list at 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.

$ python
Python 3.11.8 (main, Feb 23 2024, 16:11:29) [GCC 13.2.1 20240113] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pathlib
>>> pathlib.Path(256 * "x").is_file()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.11/pathlib.py", line 1267, in is_file
    return S_ISREG(self.stat().st_mode)
                   ^^^^^^^^^^^
  File "/usr/lib/python3.11/pathlib.py", line 1013, in stat
    return os.stat(self, follow_symlinks=follow_symlinks)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 36] File name too long: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
>>>
>>> import os
>>> os.path.isfile(256 * "x")
False



More information about the Python-list mailing list