path operations common to more than one os do not use directly the os specific modules import the appropriate functions from this module themselves does a path exist this is false for dangling symbolic links on systems that support them test whether a path exists returns false for broken symbolic links try os statpath except oserror valueerror return false return true this follows symbolic links so both islink and isdir can be true for the same path on systems that support symlinks def isfilepath is a path a directory 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 try st os stats except oserror valueerror return false return stat sisdirst stmode is a path a symbolic link this will always return false on systems where os lstat doesn t exist def islinkpath return the size of a file reported by os stat return os statfilename stsize def getmtimefilename return the last access time of a file reported by os stat return os statfilename statime def getctimefilename return the longest prefix of all list elements some people pass in a list of pathname parts to operate in an osagnostic 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 osagnostic and so they most likely won t be using an os pathlike object in the sublists 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 stino s2 stino and s1 stdev s2 stdev are two filenames really pointing to the same file def samefilef1 f2 s1 os statf1 s2 os statf2 return samestats1 s2 are two open files really referencing the same file not necessarily the same file descriptor def sameopenfilefp1 fp2 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 the separators 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 sepindex p rfindsep if altsep altsepindex p rfindaltsep sepindex maxsepindex altsepindex dotindex p rfindextsep if dotindex sepindex skip all leading dots filenameindex sepindex 1 while filenameindex dotindex if pfilenameindex filenameindex1 extsep return p dotindex pdotindex filenameindex 1 return p p 0 def checkargtypesfuncname args hasstr hasbytes false for s in args if isinstances str hasstr true elif isinstances bytes hasbytes true else raise typeerrorf funcname argument must be str bytes or f os pathlike object not s class name r from none if hasstr and hasbytes raise typeerrorcan t mix strings and bytes in path components from none does a path exist this is false for dangling symbolic links on systems that support them test whether a path exists returns false for broken symbolic links 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 is a path a directory 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 is a path a symbolic link this will always return false on systems where os lstat doesn t exist test whether a path is a symbolic link return the size of a file reported by os stat return the last modification time of a file reported by os stat return the last access time of a file reported by os stat return the metadata change time of a file reported by os stat return the longest prefix of all list elements 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 are two stat buffers obtained from stat fstat or lstat describing the same file test whether two stat buffers reference the same file 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 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 the separators 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 skip all leading dots
import os
import stat

__all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime',
           'getsize', 'isdir', 'isfile', 'islink', 'samefile', 'sameopenfile',
           'samestat']


def exists(path):
    
    try:
        os.stat(path)
    except (OSError, ValueError):
        return False
    return True


def isfile(path):
    
    try:
        st = os.stat(path)
    except (OSError, ValueError):
        return False
    return stat.S_ISREG(st.st_mode)


def isdir(s):
    
    try:
        st = os.stat(s)
    except (OSError, ValueError):
        return False
    return stat.S_ISDIR(st.st_mode)





def islink(path):
    
    try:
        st = os.lstat(path)
    except (OSError, ValueError, AttributeError):
        return False
    return stat.S_ISLNK(st.st_mode)


def getsize(filename):
    
    return os.stat(filename).st_size


def getmtime(filename):
    
    return os.stat(filename).st_mtime


def getatime(filename):
    
    return os.stat(filename).st_atime


def getctime(filename):
    
    return os.stat(filename).st_ctime



def commonprefix(m):
    "Given a list of pathnames, returns the longest common leading component"
    if not m: return ''
    
    
    
    
    if not isinstance(m[0], (list, tuple)):
        m = tuple(map(os.fspath, m))
    s1 = min(m)
    s2 = max(m)
    for i, c in enumerate(s1):
        if c != s2[i]:
            return s1[:i]
    return s1


def samestat(s1, s2):
    
    return (s1.st_ino == s2.st_ino and
            s1.st_dev == s2.st_dev)


def samefile(f1, f2):
    
    s1 = os.stat(f1)
    s2 = os.stat(f2)
    return samestat(s1, s2)


def sameopenfile(fp1, fp2):
    
    s1 = os.fstat(fp1)
    s2 = os.fstat(fp2)
    return samestat(s1, s2)

def _splitext(p, sep, altsep, extsep):
    
    sepIndex = p.rfind(sep)
    if altsep:
        altsepIndex = p.rfind(altsep)
        sepIndex = max(sepIndex, altsepIndex)

    dotIndex = p.rfind(extsep)
    if dotIndex > sepIndex:
        
        filenameIndex = sepIndex + 1
        while filenameIndex < dotIndex:
            if p[filenameIndex:filenameIndex+1] != extsep:
                return p[:dotIndex], p[dotIndex:]
            filenameIndex += 1

    return p, p[:0]

def _check_arg_types(funcname, *args):
    hasstr = hasbytes = False
    for s in args:
        if isinstance(s, str):
            hasstr = True
        elif isinstance(s, bytes):
            hasbytes = True
        else:
            raise TypeError(f'{funcname}() argument must be str, bytes, or '
                            f'os.PathLike object, not {s.__class__.__name__!r}') from None
    if hasstr and hasbytes:
        raise TypeError("Can't mix strings and bytes in path components") from None