Guido van Rossum | d3876d3 | 1996-07-23 03:47:28 +0000 | [diff] [blame] | 1 | # Module 'posixpath' -- common operations on Posix pathnames. |
| 2 | # Some of this can actually be useful on non-Posix systems too, e.g. |
| 3 | # for manipulation of the pathname component of URLs. |
| 4 | # The "os.path" name is an alias for this module on Posix systems; |
| 5 | # on other systems (e.g. Mac, Windows), os.path provides the same |
| 6 | # operations in a manner specific to that platform, and is an alias |
| 7 | # to another module (e.g. macpath, ntpath). |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | d3876d3 | 1996-07-23 03:47:28 +0000 | [diff] [blame] | 9 | import os |
Guido van Rossum | 40d9304 | 1990-10-21 16:17:34 +0000 | [diff] [blame] | 10 | import stat |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 11 | |
| 12 | |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 13 | # Normalize the case of a pathname. Trivial in Posix, string.lower on Mac. |
| 14 | # On MS-DOS this may also turn slashes into backslashes; however, other |
| 15 | # normalizations (such as optimizing '../' away) are not allowed |
| 16 | # (another function should be defined to do that). |
| 17 | |
| 18 | def normcase(s): |
| 19 | return s |
| 20 | |
| 21 | |
| 22 | # Return wheter a path is absolute. |
| 23 | # Trivial in Posix, harder on the Mac or MS-DOS. |
| 24 | |
| 25 | def isabs(s): |
| 26 | return s[:1] == '/' |
| 27 | |
| 28 | |
Barry Warsaw | 384d249 | 1997-02-18 21:53:25 +0000 | [diff] [blame] | 29 | # Join pathnames. |
| 30 | # Ignore the previous parts if a part is absolute. |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 31 | # Insert a '/' unless the first part is empty or already ends in '/'. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 32 | |
Barry Warsaw | 384d249 | 1997-02-18 21:53:25 +0000 | [diff] [blame] | 33 | def join(a, *p): |
| 34 | path = a |
| 35 | for b in p: |
| 36 | if b[:1] == '/': |
| 37 | path = b |
| 38 | elif path == '' or path[-1:] == '/': |
| 39 | path = path + b |
| 40 | else: |
| 41 | path = path + '/' + b |
| 42 | return path |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 43 | |
| 44 | |
Guido van Rossum | 2684738 | 1992-03-31 18:54:35 +0000 | [diff] [blame] | 45 | # Split a path in head (everything up to the last '/') and tail (the |
Guido van Rossum | a89b1ba | 1995-09-01 20:32:21 +0000 | [diff] [blame] | 46 | # rest). If the path ends in '/', tail will be empty. If there is no |
| 47 | # '/' in the path, head will be empty. |
| 48 | # Trailing '/'es are stripped from head unless it is the root. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 50 | def split(p): |
Guido van Rossum | a89b1ba | 1995-09-01 20:32:21 +0000 | [diff] [blame] | 51 | import string |
| 52 | i = string.rfind(p, '/') + 1 |
| 53 | head, tail = p[:i], p[i:] |
| 54 | if head and head <> '/'*len(head): |
Guido van Rossum | 2684738 | 1992-03-31 18:54:35 +0000 | [diff] [blame] | 55 | while head[-1] == '/': |
| 56 | head = head[:-1] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 57 | return head, tail |
| 58 | |
| 59 | |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 60 | # Split a path in root and extension. |
Guido van Rossum | 422869a | 1996-08-20 20:24:17 +0000 | [diff] [blame] | 61 | # The extension is everything starting at the last dot in the last |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 62 | # pathname component; the root is everything before that. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 63 | # It is always true that root + ext == p. |
| 64 | |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 65 | def splitext(p): |
| 66 | root, ext = '', '' |
| 67 | for c in p: |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 68 | if c == '/': |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 69 | root, ext = root + ext + c, '' |
Sjoerd Mullender | 4359860 | 1994-12-14 15:29:17 +0000 | [diff] [blame] | 70 | elif c == '.': |
| 71 | if ext: |
| 72 | root, ext = root + ext, c |
| 73 | else: |
| 74 | ext = c |
| 75 | elif ext: |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 76 | ext = ext + c |
| 77 | else: |
| 78 | root = root + c |
| 79 | return root, ext |
| 80 | |
| 81 | |
Guido van Rossum | 221df24 | 1995-08-07 20:17:55 +0000 | [diff] [blame] | 82 | # Split a pathname into a drive specification and the rest of the |
| 83 | # path. Useful on DOS/Windows/NT; on Unix, the drive is always empty. |
| 84 | |
| 85 | def splitdrive(p): |
| 86 | return '', p |
| 87 | |
| 88 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 89 | # Return the tail (basename) part of a path. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 90 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 91 | def basename(p): |
| 92 | return split(p)[1] |
| 93 | |
| 94 | |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 95 | # Return the head (dirname) part of a path. |
| 96 | |
| 97 | def dirname(p): |
| 98 | return split(p)[0] |
| 99 | |
| 100 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 101 | # Return the longest prefix of all list elements. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 102 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 103 | def commonprefix(m): |
| 104 | if not m: return '' |
| 105 | prefix = m[0] |
| 106 | for item in m: |
| 107 | for i in range(len(prefix)): |
| 108 | if prefix[:i+1] <> item[:i+1]: |
| 109 | prefix = prefix[:i] |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 110 | if i == 0: return '' |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 111 | break |
| 112 | return prefix |
| 113 | |
| 114 | |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 115 | # Is a path a symbolic link? |
Guido van Rossum | d3876d3 | 1996-07-23 03:47:28 +0000 | [diff] [blame] | 116 | # This will always return false on systems where os.lstat doesn't exist. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 117 | |
| 118 | def islink(path): |
| 119 | try: |
Guido van Rossum | d3876d3 | 1996-07-23 03:47:28 +0000 | [diff] [blame] | 120 | st = os.lstat(path) |
| 121 | except (os.error, AttributeError): |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 122 | return 0 |
| 123 | return stat.S_ISLNK(st[stat.ST_MODE]) |
| 124 | |
| 125 | |
| 126 | # Does a path exist? |
| 127 | # This is false for dangling symbolic links. |
| 128 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 129 | def exists(path): |
| 130 | try: |
Guido van Rossum | d3876d3 | 1996-07-23 03:47:28 +0000 | [diff] [blame] | 131 | st = os.stat(path) |
| 132 | except os.error: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 133 | return 0 |
| 134 | return 1 |
| 135 | |
| 136 | |
Guido van Rossum | d3876d3 | 1996-07-23 03:47:28 +0000 | [diff] [blame] | 137 | # Is a path a directory? |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 138 | # This follows symbolic links, so both islink() and isdir() can be true |
| 139 | # for the same path. |
| 140 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 141 | def isdir(path): |
| 142 | try: |
Guido van Rossum | d3876d3 | 1996-07-23 03:47:28 +0000 | [diff] [blame] | 143 | st = os.stat(path) |
| 144 | except os.error: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 145 | return 0 |
Guido van Rossum | 40d9304 | 1990-10-21 16:17:34 +0000 | [diff] [blame] | 146 | return stat.S_ISDIR(st[stat.ST_MODE]) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 147 | |
| 148 | |
Guido van Rossum | 2684738 | 1992-03-31 18:54:35 +0000 | [diff] [blame] | 149 | # Is a path a regular file? |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 150 | # This follows symbolic links, so both islink() and isfile() can be true |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 151 | # for the same path. |
| 152 | |
| 153 | def isfile(path): |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 154 | try: |
Guido van Rossum | d3876d3 | 1996-07-23 03:47:28 +0000 | [diff] [blame] | 155 | st = os.stat(path) |
| 156 | except os.error: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 157 | return 0 |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 158 | return stat.S_ISREG(st[stat.ST_MODE]) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 159 | |
| 160 | |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 161 | # Are two filenames really pointing to the same file? |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 162 | |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 163 | def samefile(f1, f2): |
Guido van Rossum | d3876d3 | 1996-07-23 03:47:28 +0000 | [diff] [blame] | 164 | s1 = os.stat(f1) |
| 165 | s2 = os.stat(f2) |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 166 | return samestat(s1, s2) |
| 167 | |
| 168 | |
| 169 | # Are two open files really referencing the same file? |
| 170 | # (Not necessarily the same file descriptor!) |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 171 | |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 172 | def sameopenfile(fp1, fp2): |
Guido van Rossum | d3876d3 | 1996-07-23 03:47:28 +0000 | [diff] [blame] | 173 | s1 = os.fstat(fp1) |
| 174 | s2 = os.fstat(fp2) |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 175 | return samestat(s1, s2) |
| 176 | |
| 177 | |
| 178 | # Are two stat buffers (obtained from stat, fstat or lstat) |
| 179 | # describing the same file? |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 180 | |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 181 | def samestat(s1, s2): |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 182 | return s1[stat.ST_INO] == s2[stat.ST_INO] and \ |
Guido van Rossum | 509d24a | 1992-05-06 11:36:49 +0000 | [diff] [blame] | 183 | s1[stat.ST_DEV] == s2[stat.ST_DEV] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 184 | |
| 185 | |
| 186 | # Is a path a mount point? |
Guido van Rossum | d3876d3 | 1996-07-23 03:47:28 +0000 | [diff] [blame] | 187 | # (Does this work for all UNIXes? Is it even guaranteed to work by Posix?) |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 188 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 189 | def ismount(path): |
Guido van Rossum | 509d24a | 1992-05-06 11:36:49 +0000 | [diff] [blame] | 190 | try: |
Guido van Rossum | d3876d3 | 1996-07-23 03:47:28 +0000 | [diff] [blame] | 191 | s1 = os.stat(path) |
| 192 | s2 = os.stat(join(path, '..')) |
| 193 | except os.error: |
Guido van Rossum | 509d24a | 1992-05-06 11:36:49 +0000 | [diff] [blame] | 194 | return 0 # It doesn't exist -- so not a mount point :-) |
| 195 | dev1 = s1[stat.ST_DEV] |
| 196 | dev2 = s2[stat.ST_DEV] |
| 197 | if dev1 != dev2: |
| 198 | return 1 # path/.. on a different device as path |
| 199 | ino1 = s1[stat.ST_INO] |
| 200 | ino2 = s2[stat.ST_INO] |
| 201 | if ino1 == ino2: |
| 202 | return 1 # path/.. is the same i-node as path |
| 203 | return 0 |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 204 | |
| 205 | |
| 206 | # Directory tree walk. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 207 | # For each directory under top (including top itself, but excluding |
| 208 | # '.' and '..'), func(arg, dirname, filenames) is called, where |
| 209 | # dirname is the name of the directory and filenames is the list |
| 210 | # files files (and subdirectories etc.) in the directory. |
| 211 | # The func may modify the filenames list, to implement a filter, |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 212 | # or to impose a different order of visiting. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 213 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 214 | def walk(top, func, arg): |
| 215 | try: |
Guido van Rossum | d3876d3 | 1996-07-23 03:47:28 +0000 | [diff] [blame] | 216 | names = os.listdir(top) |
| 217 | except os.error: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 218 | return |
| 219 | func(arg, top, names) |
| 220 | exceptions = ('.', '..') |
| 221 | for name in names: |
| 222 | if name not in exceptions: |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 223 | name = join(top, name) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 224 | if isdir(name) and not islink(name): |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 225 | walk(name, func, arg) |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 226 | |
| 227 | |
| 228 | # Expand paths beginning with '~' or '~user'. |
| 229 | # '~' means $HOME; '~user' means that user's home directory. |
| 230 | # If the path doesn't begin with '~', or if the user or $HOME is unknown, |
| 231 | # the path is returned unchanged (leaving error reporting to whatever |
| 232 | # function is called with the expanded path as argument). |
| 233 | # See also module 'glob' for expansion of *, ? and [...] in pathnames. |
| 234 | # (A function should also be defined to do full *sh-style environment |
| 235 | # variable expansion.) |
| 236 | |
| 237 | def expanduser(path): |
| 238 | if path[:1] <> '~': |
| 239 | return path |
| 240 | i, n = 1, len(path) |
| 241 | while i < n and path[i] <> '/': |
| 242 | i = i+1 |
| 243 | if i == 1: |
Guido van Rossum | d3876d3 | 1996-07-23 03:47:28 +0000 | [diff] [blame] | 244 | if not os.environ.has_key('HOME'): |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 245 | return path |
Guido van Rossum | d3876d3 | 1996-07-23 03:47:28 +0000 | [diff] [blame] | 246 | userhome = os.environ['HOME'] |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 247 | else: |
| 248 | import pwd |
| 249 | try: |
| 250 | pwent = pwd.getpwnam(path[1:i]) |
| 251 | except KeyError: |
| 252 | return path |
| 253 | userhome = pwent[5] |
Guido van Rossum | bbb4e10 | 1996-04-02 22:30:03 +0000 | [diff] [blame] | 254 | if userhome[-1:] == '/': i = i+1 |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 255 | return userhome + path[i:] |
Guido van Rossum | 4732ccf | 1992-08-09 13:54:50 +0000 | [diff] [blame] | 256 | |
| 257 | |
| 258 | # Expand paths containing shell variable substitutions. |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 259 | # This expands the forms $variable and ${variable} only. |
| 260 | # Non-existant variables are left unchanged. |
| 261 | |
| 262 | _varprog = None |
Guido van Rossum | 4732ccf | 1992-08-09 13:54:50 +0000 | [diff] [blame] | 263 | |
| 264 | def expandvars(path): |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 265 | global _varprog |
Guido van Rossum | 4732ccf | 1992-08-09 13:54:50 +0000 | [diff] [blame] | 266 | if '$' not in path: |
| 267 | return path |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 268 | if not _varprog: |
| 269 | import regex |
| 270 | _varprog = regex.compile('$\([a-zA-Z0-9_]+\|{[^}]*}\)') |
| 271 | i = 0 |
| 272 | while 1: |
| 273 | i = _varprog.search(path, i) |
| 274 | if i < 0: |
| 275 | break |
| 276 | name = _varprog.group(1) |
| 277 | j = i + len(_varprog.group(0)) |
| 278 | if name[:1] == '{' and name[-1:] == '}': |
| 279 | name = name[1:-1] |
Guido van Rossum | d3876d3 | 1996-07-23 03:47:28 +0000 | [diff] [blame] | 280 | if os.environ.has_key(name): |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 281 | tail = path[j:] |
Guido van Rossum | d3876d3 | 1996-07-23 03:47:28 +0000 | [diff] [blame] | 282 | path = path[:i] + os.environ[name] |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 283 | i = len(path) |
| 284 | path = path + tail |
| 285 | else: |
| 286 | i = j |
| 287 | return path |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 288 | |
| 289 | |
| 290 | # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B. |
| 291 | # It should be understood that this may change the meaning of the path |
| 292 | # if it contains symbolic links! |
| 293 | |
| 294 | def normpath(path): |
| 295 | import string |
Guido van Rossum | df56386 | 1993-07-06 15:19:36 +0000 | [diff] [blame] | 296 | # Treat initial slashes specially |
| 297 | slashes = '' |
| 298 | while path[:1] == '/': |
| 299 | slashes = slashes + '/' |
| 300 | path = path[1:] |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 301 | comps = string.splitfields(path, '/') |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 302 | i = 0 |
| 303 | while i < len(comps): |
| 304 | if comps[i] == '.': |
| 305 | del comps[i] |
| 306 | elif comps[i] == '..' and i > 0 and \ |
| 307 | comps[i-1] not in ('', '..'): |
| 308 | del comps[i-1:i+1] |
| 309 | i = i-1 |
| 310 | elif comps[i] == '' and i > 0 and comps[i-1] <> '': |
| 311 | del comps[i] |
| 312 | else: |
| 313 | i = i+1 |
| 314 | # If the path is now empty, substitute '.' |
Guido van Rossum | df56386 | 1993-07-06 15:19:36 +0000 | [diff] [blame] | 315 | if not comps and not slashes: |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 316 | comps.append('.') |
Guido van Rossum | df56386 | 1993-07-06 15:19:36 +0000 | [diff] [blame] | 317 | return slashes + string.joinfields(comps, '/') |