Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 1 | # Module 'ntpath' -- common operations on WinNT/Win95 pathnames |
| 2 | """Common pathname manipulations, WindowsNT/95 version. |
| 3 | Instead of importing this module |
| 4 | directly, import os and refer to this module as os.path. |
| 5 | """ |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 6 | |
| 7 | import os |
| 8 | import stat |
| 9 | import string |
| 10 | |
| 11 | |
Guido van Rossum | e2ad88c | 1997-08-12 14:46:58 +0000 | [diff] [blame] | 12 | # Normalize the case of a pathname and map slashes to backslashes. |
| 13 | # Other normalizations (such as optimizing '../' away) are not done |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 14 | # (this is done by normpath). |
Guido van Rossum | e2ad88c | 1997-08-12 14:46:58 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 16 | def normcase(s): |
Guido van Rossum | 16a0bc2 | 1998-02-18 13:48:31 +0000 | [diff] [blame] | 17 | """Normalize case of pathname. |
| 18 | |
| 19 | Makes all characters lowercase and all slashes into backslashes. |
| 20 | |
| 21 | """ |
| 22 | return string.lower(string.replace(s, "/", "\\")) |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | 77e1db3 | 1997-06-02 23:11:57 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 25 | # Return wheter a path is absolute. |
| 26 | # Trivial in Posix, harder on the Mac or MS-DOS. |
| 27 | # For DOS it is absolute if it starts with a slash or backslash (current |
| 28 | # volume), or if a pathname after the volume letter and colon starts with |
| 29 | # a slash or backslash. |
| 30 | |
| 31 | def isabs(s): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 32 | """Test whether a path is absolute""" |
| 33 | s = splitdrive(s)[1] |
| 34 | return s != '' and s[:1] in '/\\' |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 35 | |
| 36 | |
Guido van Rossum | 77e1db3 | 1997-06-02 23:11:57 +0000 | [diff] [blame] | 37 | # Join two (or more) paths. |
| 38 | |
Barry Warsaw | 384d249 | 1997-02-18 21:53:25 +0000 | [diff] [blame] | 39 | def join(a, *p): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 40 | """Join two or more pathname components, inserting "\\" as needed""" |
| 41 | path = a |
| 42 | for b in p: |
| 43 | if isabs(b): |
| 44 | path = b |
| 45 | elif path == '' or path[-1:] in '/\\': |
| 46 | path = path + b |
| 47 | else: |
| 48 | path = path + os.sep + b |
| 49 | return path |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 50 | |
| 51 | |
| 52 | # Split a path in a drive specification (a drive letter followed by a |
| 53 | # colon) and the path specification. |
| 54 | # It is always true that drivespec + pathspec == p |
| 55 | def splitdrive(p): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 56 | """Split a pathname into drive and path specifiers. Returns a 2-tuple |
| 57 | "(drive,path)"; either part may be empty""" |
| 58 | if p[1:2] == ':': |
| 59 | return p[0:2], p[2:] |
| 60 | return '', p |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 61 | |
| 62 | |
| 63 | # Split a path in head (everything up to the last '/') and tail (the |
| 64 | # rest). If the original path ends in '/' but is not the root, this |
| 65 | # '/' is stripped. After the trailing '/' is stripped, the invariant |
| 66 | # join(head, tail) == p holds. |
| 67 | # The resulting head won't end in '/' unless it is the root. |
| 68 | |
| 69 | def split(p): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 70 | """Split a pathname. Returns tuple "(head, tail)" where "tail" is |
| 71 | everything after the final slash. Either part may be empty""" |
| 72 | d, p = splitdrive(p) |
| 73 | slashes = '' |
| 74 | while p and p[-1:] in '/\\': |
| 75 | slashes = slashes + p[-1] |
| 76 | p = p[:-1] |
| 77 | if p == '': |
| 78 | p = p + slashes |
| 79 | head, tail = '', '' |
| 80 | for c in p: |
| 81 | tail = tail + c |
| 82 | if c in '/\\': |
| 83 | head, tail = head + tail, '' |
| 84 | slashes = '' |
| 85 | while head and head[-1:] in '/\\': |
| 86 | slashes = slashes + head[-1] |
| 87 | head = head[:-1] |
| 88 | if head == '': |
| 89 | head = head + slashes |
| 90 | return d + head, tail |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 91 | |
| 92 | |
| 93 | # Split a path in root and extension. |
Guido van Rossum | 73e122f | 1997-01-22 00:17:26 +0000 | [diff] [blame] | 94 | # The extension is everything starting at the last dot in the last |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 95 | # pathname component; the root is everything before that. |
| 96 | # It is always true that root + ext == p. |
| 97 | |
| 98 | def splitext(p): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 99 | """Split the extension from a pathname. Extension is everything from the |
| 100 | last dot to the end. Returns "(root, ext)", either part may be empty""" |
| 101 | root, ext = '', '' |
| 102 | for c in p: |
| 103 | if c in ['/','\\']: |
| 104 | root, ext = root + ext + c, '' |
| 105 | elif c == '.': |
| 106 | if ext: |
| 107 | root, ext = root + ext, c |
| 108 | else: |
| 109 | ext = c |
| 110 | elif ext: |
| 111 | ext = ext + c |
| 112 | else: |
| 113 | root = root + c |
| 114 | return root, ext |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 115 | |
| 116 | |
| 117 | # Return the tail (basename) part of a path. |
| 118 | |
| 119 | def basename(p): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 120 | """Returns the final component of a pathname""" |
| 121 | return split(p)[1] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 122 | |
| 123 | |
| 124 | # Return the head (dirname) part of a path. |
| 125 | |
| 126 | def dirname(p): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 127 | """Returns the directory component of a pathname""" |
| 128 | return split(p)[0] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 129 | |
| 130 | |
| 131 | # Return the longest prefix of all list elements. |
| 132 | |
| 133 | def commonprefix(m): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 134 | "Given a list of pathnames, returns the longest common leading component" |
| 135 | if not m: return '' |
| 136 | prefix = m[0] |
| 137 | for item in m: |
| 138 | for i in range(len(prefix)): |
| 139 | if prefix[:i+1] <> item[:i+1]: |
| 140 | prefix = prefix[:i] |
| 141 | if i == 0: return '' |
| 142 | break |
| 143 | return prefix |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 144 | |
| 145 | |
| 146 | # Is a path a symbolic link? |
| 147 | # This will always return false on systems where posix.lstat doesn't exist. |
| 148 | |
| 149 | def islink(path): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 150 | """Test for symbolic link. On WindowsNT/95 always returns false""" |
| 151 | return 0 |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 152 | |
| 153 | |
| 154 | # Does a path exist? |
| 155 | # This is false for dangling symbolic links. |
| 156 | |
| 157 | def exists(path): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 158 | """Test whether a path exists""" |
| 159 | try: |
| 160 | st = os.stat(path) |
| 161 | except os.error: |
| 162 | return 0 |
| 163 | return 1 |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 164 | |
| 165 | |
| 166 | # Is a path a dos directory? |
| 167 | # This follows symbolic links, so both islink() and isdir() can be true |
| 168 | # for the same path. |
| 169 | |
| 170 | def isdir(path): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 171 | """Test whether a path is a directory""" |
| 172 | try: |
| 173 | st = os.stat(path) |
| 174 | except os.error: |
| 175 | return 0 |
| 176 | return stat.S_ISDIR(st[stat.ST_MODE]) |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 177 | |
| 178 | |
| 179 | # Is a path a regular file? |
| 180 | # This follows symbolic links, so both islink() and isdir() can be true |
| 181 | # for the same path. |
| 182 | |
| 183 | def isfile(path): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 184 | """Test whether a path is a regular file""" |
| 185 | try: |
| 186 | st = os.stat(path) |
| 187 | except os.error: |
| 188 | return 0 |
| 189 | return stat.S_ISREG(st[stat.ST_MODE]) |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 190 | |
| 191 | |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 192 | # Is a path a mount point? |
| 193 | # XXX This degenerates in: 'is this the root?' on DOS |
| 194 | |
| 195 | def ismount(path): |
Guido van Rossum | ca99c2c | 1998-01-19 22:25:59 +0000 | [diff] [blame] | 196 | """Test whether a path is a mount point (defined as root of drive)""" |
| 197 | p = splitdrive(path)[1] |
| 198 | return len(p)==1 and p[0] in '/\\' |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 199 | |
| 200 | |
| 201 | # Directory tree walk. |
| 202 | # For each directory under top (including top itself, but excluding |
| 203 | # '.' and '..'), func(arg, dirname, filenames) is called, where |
| 204 | # dirname is the name of the directory and filenames is the list |
| 205 | # files files (and subdirectories etc.) in the directory. |
| 206 | # The func may modify the filenames list, to implement a filter, |
| 207 | # or to impose a different order of visiting. |
| 208 | |
| 209 | def walk(top, func, arg): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 210 | """walk(top,func,args) calls func(arg, d, files) for each directory "d" |
| 211 | in the tree rooted at "top" (including "top" itself). "files" is a list |
| 212 | of all the files and subdirs in directory "d". |
| 213 | """ |
| 214 | try: |
| 215 | names = os.listdir(top) |
| 216 | except os.error: |
| 217 | return |
| 218 | func(arg, top, names) |
| 219 | exceptions = ('.', '..') |
| 220 | for name in names: |
| 221 | if name not in exceptions: |
| 222 | name = join(top, name) |
| 223 | if isdir(name): |
| 224 | walk(name, func, arg) |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 225 | |
| 226 | |
| 227 | # Expand paths beginning with '~' or '~user'. |
| 228 | # '~' means $HOME; '~user' means that user's home directory. |
| 229 | # If the path doesn't begin with '~', or if the user or $HOME is unknown, |
| 230 | # the path is returned unchanged (leaving error reporting to whatever |
| 231 | # function is called with the expanded path as argument). |
| 232 | # See also module 'glob' for expansion of *, ? and [...] in pathnames. |
| 233 | # (A function should also be defined to do full *sh-style environment |
| 234 | # variable expansion.) |
| 235 | |
| 236 | def expanduser(path): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 237 | """Expand ~ and ~user constructions. If user or $HOME is unknown, |
| 238 | do nothing""" |
| 239 | if path[:1] <> '~': |
| 240 | return path |
| 241 | i, n = 1, len(path) |
| 242 | while i < n and path[i] not in '/\\': |
| 243 | i = i+1 |
| 244 | if i == 1: |
| 245 | if os.environ.has_key('HOME'): |
| 246 | userhome = os.environ['HOME'] |
| 247 | elif not os.environ.has_key('HOMEPATH'): |
| 248 | return path |
| 249 | else: |
| 250 | try: |
| 251 | drive=os.environ['HOMEDRIVE'] |
| 252 | except KeyError: |
| 253 | drive = '' |
| 254 | userhome = join(drive, os.environ['HOMEPATH']) |
| 255 | else: |
| 256 | return path |
| 257 | return userhome + path[i:] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 258 | |
| 259 | |
| 260 | # Expand paths containing shell variable substitutions. |
| 261 | # The following rules apply: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 262 | # - no expansion within single quotes |
| 263 | # - no escape character, except for '$$' which is translated into '$' |
| 264 | # - ${varname} is accepted. |
| 265 | # - varnames can be made out of letters, digits and the character '_' |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 266 | # XXX With COMMAND.COM you can use any characters in a variable name, |
| 267 | # XXX except '^|<>='. |
| 268 | |
| 269 | varchars = string.letters + string.digits + '_-' |
| 270 | |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 271 | def expandvars(path): |
| 272 | """Expand shell variables of form $var and ${var}. Unknown variables |
| 273 | are left unchanged""" |
| 274 | if '$' not in path: |
| 275 | return path |
| 276 | res = '' |
| 277 | index = 0 |
| 278 | pathlen = len(path) |
| 279 | while index < pathlen: |
| 280 | c = path[index] |
| 281 | if c == '\'': # no expansion within single quotes |
| 282 | path = path[index + 1:] |
| 283 | pathlen = len(path) |
| 284 | try: |
| 285 | index = string.index(path, '\'') |
| 286 | res = res + '\'' + path[:index + 1] |
| 287 | except string.index_error: |
| 288 | res = res + path |
| 289 | index = pathlen -1 |
| 290 | elif c == '$': # variable or '$$' |
| 291 | if path[index + 1:index + 2] == '$': |
| 292 | res = res + c |
| 293 | index = index + 1 |
| 294 | elif path[index + 1:index + 2] == '{': |
| 295 | path = path[index+2:] |
| 296 | pathlen = len(path) |
| 297 | try: |
| 298 | index = string.index(path, '}') |
| 299 | var = path[:index] |
| 300 | if os.environ.has_key(var): |
| 301 | res = res + os.environ[var] |
| 302 | except string.index_error: |
| 303 | res = res + path |
| 304 | index = pathlen - 1 |
| 305 | else: |
| 306 | var = '' |
| 307 | index = index + 1 |
| 308 | c = path[index:index + 1] |
| 309 | while c != '' and c in varchars: |
| 310 | var = var + c |
| 311 | index = index + 1 |
| 312 | c = path[index:index + 1] |
| 313 | if os.environ.has_key(var): |
| 314 | res = res + os.environ[var] |
| 315 | if c != '': |
| 316 | res = res + c |
| 317 | else: |
| 318 | res = res + c |
| 319 | index = index + 1 |
| 320 | return res |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 321 | |
| 322 | |
| 323 | # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B. |
Guido van Rossum | 3df7b5a | 1996-08-26 16:35:26 +0000 | [diff] [blame] | 324 | # Previously, this function also truncated pathnames to 8+3 format, |
| 325 | # but as this module is called "ntpath", that's obviously wrong! |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 326 | |
| 327 | def normpath(path): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 328 | """Normalize path, eliminating double slashes, etc.""" |
Guido van Rossum | 16a0bc2 | 1998-02-18 13:48:31 +0000 | [diff] [blame] | 329 | path = string.replace(path, "/", "\\") |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 330 | prefix, path = splitdrive(path) |
| 331 | while path[:1] == os.sep: |
| 332 | prefix = prefix + os.sep |
| 333 | path = path[1:] |
| 334 | comps = string.splitfields(path, os.sep) |
| 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 prefix and not comps: |
| 348 | comps.append('.') |
| 349 | return prefix + string.joinfields(comps, os.sep) |