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