blob: 73d7b26b3e27a137a1ff7897bb3143d5b7fd75bd [file] [log] [blame]
Tim Petersc4a70fb2006-09-05 02:22:17 +00001"""
2Path operations common to more than one OS
3Do not use directly. The OS specific modules import the appropriate
4functions from this module themselves.
5"""
6import os
7import 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.
15def exists(path):
16 """Test whether a path exists. Returns False for broken symbolic links"""
17 try:
18 st = os.stat(path)
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
26def 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
38def 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
47def getsize(filename):
48 """Return the size of a file, reported by os.stat()."""
49 return os.stat(filename).st_size
50
51
52def getmtime(filename):
53 """Return the last modification time of a file, reported by os.stat()."""
54 return os.stat(filename).st_mtime
55
56
57def getatime(filename):
58 """Return the last access time of a file, reported by os.stat()."""
59 return os.stat(filename).st_atime
60
61
62def 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.
68def 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)
Raymond Hettinger8be9ab82007-05-14 20:52:31 +000073 for i, c in enumerate(s1):
74 if c != s2[i]:
Tim Petersc4a70fb2006-09-05 02:22:17 +000075 return s1[:i]
Raymond Hettinger8be9ab82007-05-14 20:52:31 +000076 return s1
Martin v. Löwis05c075d2007-03-07 11:04:33 +000077
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
85def _splitext(p, sep, altsep, extsep):
Tim Petersea5962f2007-03-12 18:07:52 +000086 """Split the extension from a pathname.
Martin v. Löwis05c075d2007-03-07 11:04:33 +000087
88 Extension is everything from the last dot to the end, ignoring
89 leading dots. Returns "(root, ext)"; ext may be empty."""
90
91 sepIndex = p.rfind(sep)
92 if altsep:
93 altsepIndex = p.rfind(altsep)
94 sepIndex = max(sepIndex, altsepIndex)
95
96 dotIndex = p.rfind(extsep)
97 if dotIndex > sepIndex:
98 # skip all leading dots
99 filenameIndex = sepIndex + 1
100 while filenameIndex < dotIndex:
101 if p[filenameIndex] != extsep:
102 return p[:dotIndex], p[dotIndex:]
103 filenameIndex += 1
104
105 return p, ''