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

Thomas Passin list1 at tompassin.net
Fri Mar 8 13:35:47 EST 2024


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.  Either that or that path name exists and throws some 
kind of unexpected exception.

The Python docs say

"Return True if the path points to a regular file (or a symbolic link 
pointing to a regular file), False if it points to another kind of file.

False is also returned if the path doesn’t exist or is a broken symlink; 
other errors (such as permission errors) are propagated"


More information about the Python-list mailing list