Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1 | """ |
| 2 | Path operations common to more than one OS |
| 3 | Do not use directly. The OS specific modules import the appropriate |
| 4 | functions from this module themselves. |
| 5 | """ |
| 6 | import os |
| 7 | import stat |
| 8 | |
| 9 | __all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime', |
| 10 | 'getsize', 'isdir', 'isfile'] |
| 11 | |
| 12 | |
| 13 | # Does a path exist? |
| 14 | # This is false for dangling symbolic links on systems that support them. |
| 15 | def exists(path): |
| 16 | """Test whether a path exists. Returns False for broken symbolic links""" |
| 17 | try: |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 18 | os.stat(path) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 19 | except os.error: |
| 20 | return False |
| 21 | return True |
| 22 | |
| 23 | |
| 24 | # This follows symbolic links, so both islink() and isdir() can be true |
| 25 | # for the same path ono systems that support symlinks |
| 26 | def isfile(path): |
| 27 | """Test whether a path is a regular file""" |
| 28 | try: |
| 29 | st = os.stat(path) |
| 30 | except os.error: |
| 31 | return False |
| 32 | return stat.S_ISREG(st.st_mode) |
| 33 | |
| 34 | |
| 35 | # Is a path a directory? |
| 36 | # This follows symbolic links, so both islink() and isdir() |
| 37 | # can be true for the same path on systems that support symlinks |
| 38 | def isdir(s): |
| 39 | """Return true if the pathname refers to an existing directory.""" |
| 40 | try: |
| 41 | st = os.stat(s) |
| 42 | except os.error: |
| 43 | return False |
| 44 | return stat.S_ISDIR(st.st_mode) |
| 45 | |
| 46 | |
| 47 | def getsize(filename): |
| 48 | """Return the size of a file, reported by os.stat().""" |
| 49 | return os.stat(filename).st_size |
| 50 | |
| 51 | |
| 52 | def getmtime(filename): |
| 53 | """Return the last modification time of a file, reported by os.stat().""" |
| 54 | return os.stat(filename).st_mtime |
| 55 | |
| 56 | |
| 57 | def getatime(filename): |
| 58 | """Return the last access time of a file, reported by os.stat().""" |
| 59 | return os.stat(filename).st_atime |
| 60 | |
| 61 | |
| 62 | def getctime(filename): |
| 63 | """Return the metadata change time of a file, reported by os.stat().""" |
| 64 | return os.stat(filename).st_ctime |
| 65 | |
| 66 | |
| 67 | # Return the longest prefix of all list elements. |
| 68 | def commonprefix(m): |
| 69 | "Given a list of pathnames, returns the longest common leading component" |
| 70 | if not m: return '' |
| 71 | s1 = min(m) |
| 72 | s2 = max(m) |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 73 | for i, c in enumerate(s1): |
| 74 | if c != s2[i]: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 75 | return s1[:i] |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 76 | return s1 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 77 | |
| 78 | # Split a path in root and extension. |
| 79 | # The extension is everything starting at the last dot in the last |
| 80 | # pathname component; the root is everything before that. |
| 81 | # It is always true that root + ext == p. |
| 82 | |
| 83 | # Generic implementation of splitext, to be parametrized with |
| 84 | # the separators |
| 85 | def _splitext(p, sep, altsep, extsep): |
| 86 | """Split the extension from a pathname. |
| 87 | |
| 88 | Extension is everything from the last dot to the end, ignoring |
| 89 | leading dots. Returns "(root, ext)"; ext may be empty.""" |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 90 | # NOTE: This code must work for text and bytes strings. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 91 | |
| 92 | sepIndex = p.rfind(sep) |
| 93 | if altsep: |
| 94 | altsepIndex = p.rfind(altsep) |
| 95 | sepIndex = max(sepIndex, altsepIndex) |
| 96 | |
| 97 | dotIndex = p.rfind(extsep) |
| 98 | if dotIndex > sepIndex: |
| 99 | # skip all leading dots |
| 100 | filenameIndex = sepIndex + 1 |
| 101 | while filenameIndex < dotIndex: |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 102 | if p[filenameIndex:filenameIndex+1] != extsep: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 103 | return p[:dotIndex], p[dotIndex:] |
| 104 | filenameIndex += 1 |
| 105 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 106 | return p, p[:0] |