Path operations common to more than one OS
Do not use directly. The OS specific modules import the appropriate
functions from this module themselves.
__all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime',
'getsize', 'isdir', 'isfile', 'samefile', 'sameopenfile',
# This is false for dangling symbolic links on systems that support them.
"""Test whether a path exists. Returns False for broken symbolic links"""
except (OSError, ValueError):
# This follows symbolic links, so both islink() and isdir() can be true
# for the same path on systems that support symlinks
"""Test whether a path is a regular file"""
except (OSError, ValueError):
return stat.S_ISREG(st.st_mode)
# This follows symbolic links, so both islink() and isdir()
# can be true for the same path on systems that support symlinks
"""Return true if the pathname refers to an existing directory."""
except (OSError, ValueError):
return stat.S_ISDIR(st.st_mode)
"""Return the size of a file, reported by os.stat()."""
return os.stat(filename).st_size
"""Return the last modification time of a file, reported by os.stat()."""
return os.stat(filename).st_mtime
"""Return the last access time of a file, reported by os.stat()."""
return os.stat(filename).st_atime
"""Return the metadata change time of a file, reported by os.stat()."""
return os.stat(filename).st_ctime
# Return the longest prefix of all list elements.
"Given a list of pathnames, returns the longest common leading component"
# Some people pass in a list of pathname parts to operate in an OS-agnostic
# fashion; don't try to translate in that case as that's an abuse of the
# API and they are already doing what they need to be OS-agnostic and so
# they most likely won't be using an os.PathLike object in the sublists.
if not isinstance(m[0], (list, tuple)):
m = tuple(map(os.fspath, m))
for i, c in enumerate(s1):
# Are two stat buffers (obtained from stat, fstat or lstat)
# describing the same file?
"""Test whether two stat buffers reference the same file"""
return (s1.st_ino == s2.st_ino and
# Are two filenames really pointing to the same file?
"""Test whether two pathnames reference the same actual file or directory
This is determined by the device number and i-node number and
raises an exception if an os.stat() call on either pathname fails.
# Are two open files really referencing the same file?
# (Not necessarily the same file descriptor!)
def sameopenfile(fp1, fp2):
"""Test whether two open file objects reference the same file"""
# Split a path in root and extension.
# The extension is everything starting at the last dot in the last
# pathname component; the root is everything before that.
# It is always true that root + ext == p.
# Generic implementation of splitext, to be parametrized with
def _splitext(p, sep, altsep, extsep):
"""Split the extension from a pathname.
Extension is everything from the last dot to the end, ignoring
leading dots. Returns "(root, ext)"; ext may be empty."""
# NOTE: This code must work for text and bytes strings.
altsepIndex = p.rfind(altsep)
sepIndex = max(sepIndex, altsepIndex)
dotIndex = p.rfind(extsep)
filenameIndex = sepIndex + 1
while filenameIndex < dotIndex:
if p[filenameIndex:filenameIndex+1] != extsep:
return p[:dotIndex], p[dotIndex:]
def _check_arg_types(funcname, *args):
hasstr = hasbytes = False
elif isinstance(s, bytes):
raise TypeError(f'{funcname}() argument must be str, bytes, or '
f'os.PathLike object, not {s.__class__.__name__!r}') from None
raise TypeError("Can't mix strings and bytes in path components") from None