genericpath.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. __all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime',
  9. 'getsize', 'isdir', 'isfile']
  10. try:
  11. _unicode = unicode
  12. except NameError:
  13. # If Python is built without Unicode support, the unicode type
  14. # will not exist. Fake one.
  15. class _unicode(object):
  16. pass
  17. # Does a path exist?
  18. # This is false for dangling symbolic links on systems that support them.
  19. def exists(path):
  20. """Test whether a path exists. Returns False for broken symbolic links"""
  21. try:
  22. os.stat(path)
  23. except os.error:
  24. return False
  25. return True
  26. # This follows symbolic links, so both islink() and isdir() can be true
  27. # for the same path on systems that support symlinks
  28. def isfile(path):
  29. """Test whether a path is a regular file"""
  30. try:
  31. st = os.stat(path)
  32. except os.error:
  33. return False
  34. return stat.S_ISREG(st.st_mode)
  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. def getsize(filename):
  46. """Return the size of a file, reported by os.stat()."""
  47. return os.stat(filename).st_size
  48. def getmtime(filename):
  49. """Return the last modification time of a file, reported by os.stat()."""
  50. return os.stat(filename).st_mtime
  51. def getatime(filename):
  52. """Return the last access time of a file, reported by os.stat()."""
  53. return os.stat(filename).st_atime
  54. def getctime(filename):
  55. """Return the metadata change time of a file, reported by os.stat()."""
  56. return os.stat(filename).st_ctime
  57. # Return the longest prefix of all list elements.
  58. def commonprefix(m):
  59. "Given a list of pathnames, returns the longest common leading component"
  60. if not m: return ''
  61. s1 = min(m)
  62. s2 = max(m)
  63. for i, c in enumerate(s1):
  64. if c != s2[i]:
  65. return s1[:i]
  66. return s1
  67. # Split a path in root and extension.
  68. # The extension is everything starting at the last dot in the last
  69. # pathname component; the root is everything before that.
  70. # It is always true that root + ext == p.
  71. # Generic implementation of splitext, to be parametrized with
  72. # the separators
  73. def _splitext(p, sep, altsep, extsep):
  74. """Split the extension from a pathname.
  75. Extension is everything from the last dot to the end, ignoring
  76. leading dots. Returns "(root, ext)"; ext may be empty."""
  77. sepIndex = p.rfind(sep)
  78. if altsep:
  79. altsepIndex = p.rfind(altsep)
  80. sepIndex = max(sepIndex, altsepIndex)
  81. dotIndex = p.rfind(extsep)
  82. if dotIndex > sepIndex:
  83. # skip all leading dots
  84. filenameIndex = sepIndex + 1
  85. while filenameIndex < dotIndex:
  86. if p[filenameIndex] != extsep:
  87. return p[:dotIndex], p[dotIndex:]
  88. filenameIndex += 1
  89. return p, ''