/Lib/genericpath.py

http://unladen-swallow.googlecode.com/ · Python · 105 lines · 53 code · 22 blank · 30 comment · 13 complexity · ed542e3eab9574117d7deef7b00352fd MD5 · raw file

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