| 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 |
try: |
| 14 |
_unicode = unicode |
| 15 |
except NameError: |
| 16 |
|
| 17 |
|
| 18 |
class _unicode(object): |
| 19 |
pass |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
def exists(path): |
| 24 |
"""Test whether a path exists. Returns False for broken symbolic links""" |
| 25 |
try: |
| 26 |
os.stat(path) |
| 27 |
except os.error: |
| 28 |
return False |
| 29 |
return True |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
def isfile(path): |
| 35 |
"""Test whether a path is a regular file""" |
| 36 |
try: |
| 37 |
st = os.stat(path) |
| 38 |
except os.error: |
| 39 |
return False |
| 40 |
return stat.S_ISREG(st.st_mode) |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
def isdir(s): |
| 47 |
"""Return true if the pathname refers to an existing directory.""" |
| 48 |
try: |
| 49 |
st = os.stat(s) |
| 50 |
except os.error: |
| 51 |
return False |
| 52 |
return stat.S_ISDIR(st.st_mode) |
| 53 |
|
| 54 |
|
| 55 |
def getsize(filename): |
| 56 |
"""Return the size of a file, reported by os.stat().""" |
| 57 |
return os.stat(filename).st_size |
| 58 |
|
| 59 |
|
| 60 |
def getmtime(filename): |
| 61 |
"""Return the last modification time of a file, reported by os.stat().""" |
| 62 |
return os.stat(filename).st_mtime |
| 63 |
|
| 64 |
|
| 65 |
def getatime(filename): |
| 66 |
"""Return the last access time of a file, reported by os.stat().""" |
| 67 |
return os.stat(filename).st_atime |
| 68 |
|
| 69 |
|
| 70 |
def getctime(filename): |
| 71 |
"""Return the metadata change time of a file, reported by os.stat().""" |
| 72 |
return os.stat(filename).st_ctime |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
def commonprefix(m): |
| 77 |
"Given a list of pathnames, returns the longest common leading component" |
| 78 |
if not m: return '' |
| 79 |
s1 = min(m) |
| 80 |
s2 = max(m) |
| 81 |
for i, c in enumerate(s1): |
| 82 |
if c != s2[i]: |
| 83 |
return s1[:i] |
| 84 |
return s1 |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
def _splitext(p, sep, altsep, extsep): |
| 94 |
"""Split the extension from a pathname. |
| 95 |
|
| 96 |
Extension is everything from the last dot to the end, ignoring |
| 97 |
leading dots. Returns "(root, ext)"; ext may be empty.""" |
| 98 |
|
| 99 |
sepIndex = p.rfind(sep) |
| 100 |
if altsep: |
| 101 |
altsepIndex = p.rfind(altsep) |
| 102 |
sepIndex = max(sepIndex, altsepIndex) |
| 103 |
|
| 104 |
dotIndex = p.rfind(extsep) |
| 105 |
if dotIndex > sepIndex: |
| 106 |
|
| 107 |
filenameIndex = sepIndex + 1 |
| 108 |
while filenameIndex < dotIndex: |
| 109 |
if p[filenameIndex] != extsep: |
| 110 |
return p[:dotIndex], p[dotIndex:] |
| 111 |
filenameIndex += 1 |
| 112 |
|
| 113 |
return p, '' |