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