Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 1 | # Module 'ntpath' -- common operations on WinNT/Win95 pathnames |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 2 | """Common pathname manipulations, WindowsNT/95 version. |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 3 | |
| 4 | Instead of importing this module directly, import os and refer to this |
| 5 | module as os.path. |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 6 | """ |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 7 | |
| 8 | import os |
| 9 | import stat |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 10 | import sys |
Skip Montanaro | 4d5d5bf | 2000-07-13 01:01:03 +0000 | [diff] [blame] | 11 | |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame] | 12 | __all__ = ["normcase","isabs","join","splitdrive","split","splitext", |
| 13 | "basename","dirname","commonprefix","getsize","getmtime", |
Martin v. Löwis | 96a60e4 | 2002-12-31 13:11:54 +0000 | [diff] [blame] | 14 | "getatime","getctime", "islink","exists","isdir","isfile","ismount", |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 15 | "walk","expanduser","expandvars","normpath","abspath","splitunc", |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 16 | "curdir","pardir","sep","pathsep","defpath","altsep","extsep", |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 17 | "devnull","realpath","supports_unicode_filenames"] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 18 | |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 19 | # strings representing various path-related bits and pieces |
| 20 | curdir = '.' |
| 21 | pardir = '..' |
| 22 | extsep = '.' |
| 23 | sep = '\\' |
| 24 | pathsep = ';' |
Skip Montanaro | 9ddac3e | 2003-03-28 22:23:24 +0000 | [diff] [blame] | 25 | altsep = '/' |
Andrew MacIntyre | 437966c | 2003-02-17 09:17:50 +0000 | [diff] [blame] | 26 | defpath = '.;C:\\bin' |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 27 | if 'ce' in sys.builtin_module_names: |
| 28 | defpath = '\\Windows' |
| 29 | elif 'os2' in sys.builtin_module_names: |
Andrew MacIntyre | 437966c | 2003-02-17 09:17:50 +0000 | [diff] [blame] | 30 | # OS/2 w/ VACPP |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 31 | altsep = '/' |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 32 | devnull = 'nul' |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | e2ad88c | 1997-08-12 14:46:58 +0000 | [diff] [blame] | 34 | # Normalize the case of a pathname and map slashes to backslashes. |
| 35 | # Other normalizations (such as optimizing '../' away) are not done |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 36 | # (this is done by normpath). |
Guido van Rossum | e2ad88c | 1997-08-12 14:46:58 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 38 | def normcase(s): |
Guido van Rossum | 16a0bc2 | 1998-02-18 13:48:31 +0000 | [diff] [blame] | 39 | """Normalize case of pathname. |
| 40 | |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 41 | Makes all characters lowercase and all slashes into backslashes.""" |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 42 | return s.replace("/", "\\").lower() |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | 77e1db3 | 1997-06-02 23:11:57 +0000 | [diff] [blame] | 44 | |
Fred Drake | ef0b5dd | 2000-02-17 17:30:40 +0000 | [diff] [blame] | 45 | # Return whether a path is absolute. |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 46 | # Trivial in Posix, harder on the Mac or MS-DOS. |
| 47 | # For DOS it is absolute if it starts with a slash or backslash (current |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 48 | # volume), or if a pathname after the volume letter and colon / UNC resource |
| 49 | # starts with a slash or backslash. |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 50 | |
| 51 | def isabs(s): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 52 | """Test whether a path is absolute""" |
| 53 | s = splitdrive(s)[1] |
| 54 | return s != '' and s[:1] in '/\\' |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 55 | |
| 56 | |
Guido van Rossum | 77e1db3 | 1997-06-02 23:11:57 +0000 | [diff] [blame] | 57 | # Join two (or more) paths. |
| 58 | |
Barry Warsaw | 384d249 | 1997-02-18 21:53:25 +0000 | [diff] [blame] | 59 | def join(a, *p): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 60 | """Join two or more pathname components, inserting "\\" as needed""" |
| 61 | path = a |
| 62 | for b in p: |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 63 | b_wins = 0 # set to 1 iff b makes path irrelevant |
| 64 | if path == "": |
| 65 | b_wins = 1 |
Tim Peters | 1bdd0f2 | 2001-07-19 17:18:18 +0000 | [diff] [blame] | 66 | |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 67 | elif isabs(b): |
| 68 | # This probably wipes out path so far. However, it's more |
| 69 | # complicated if path begins with a drive letter: |
| 70 | # 1. join('c:', '/a') == 'c:/a' |
| 71 | # 2. join('c:/', '/a') == 'c:/a' |
| 72 | # But |
| 73 | # 3. join('c:/a', '/b') == '/b' |
| 74 | # 4. join('c:', 'd:/') = 'd:/' |
| 75 | # 5. join('c:/', 'd:/') = 'd:/' |
| 76 | if path[1:2] != ":" or b[1:2] == ":": |
| 77 | # Path doesn't start with a drive letter, or cases 4 and 5. |
| 78 | b_wins = 1 |
Tim Peters | 1bdd0f2 | 2001-07-19 17:18:18 +0000 | [diff] [blame] | 79 | |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 80 | # Else path has a drive letter, and b doesn't but is absolute. |
| 81 | elif len(path) > 3 or (len(path) == 3 and |
| 82 | path[-1] not in "/\\"): |
| 83 | # case 3 |
| 84 | b_wins = 1 |
Tim Peters | 1bdd0f2 | 2001-07-19 17:18:18 +0000 | [diff] [blame] | 85 | |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 86 | if b_wins: |
| 87 | path = b |
| 88 | else: |
| 89 | # Join, and ensure there's a separator. |
| 90 | assert len(path) > 0 |
| 91 | if path[-1] in "/\\": |
| 92 | if b and b[0] in "/\\": |
| 93 | path += b[1:] |
| 94 | else: |
| 95 | path += b |
| 96 | elif path[-1] == ":": |
| 97 | path += b |
| 98 | elif b: |
| 99 | if b[0] in "/\\": |
| 100 | path += b |
| 101 | else: |
| 102 | path += "\\" + b |
Tim Peters | 6a3e5f1 | 2001-11-05 21:25:02 +0000 | [diff] [blame] | 103 | else: |
| 104 | # path is not empty and does not end with a backslash, |
| 105 | # but b is empty; since, e.g., split('a/') produces |
| 106 | # ('a', ''), it's best if join() adds a backslash in |
| 107 | # this case. |
| 108 | path += '\\' |
Tim Peters | 1bdd0f2 | 2001-07-19 17:18:18 +0000 | [diff] [blame] | 109 | |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 110 | return path |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 111 | |
| 112 | |
| 113 | # Split a path in a drive specification (a drive letter followed by a |
Guido van Rossum | f3c695c | 1999-04-06 19:32:19 +0000 | [diff] [blame] | 114 | # colon) and the path specification. |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 115 | # It is always true that drivespec + pathspec == p |
| 116 | def splitdrive(p): |
Guido van Rossum | f3c695c | 1999-04-06 19:32:19 +0000 | [diff] [blame] | 117 | """Split a pathname into drive and path specifiers. Returns a 2-tuple |
| 118 | "(drive,path)"; either part may be empty""" |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 119 | if p[1:2] == ':': |
| 120 | return p[0:2], p[2:] |
Guido van Rossum | f3c695c | 1999-04-06 19:32:19 +0000 | [diff] [blame] | 121 | return '', p |
| 122 | |
| 123 | |
| 124 | # Parse UNC paths |
| 125 | def splitunc(p): |
| 126 | """Split a pathname into UNC mount point and relative path specifiers. |
| 127 | |
| 128 | Return a 2-tuple (unc, rest); either part may be empty. |
| 129 | If unc is not empty, it has the form '//host/mount' (or similar |
| 130 | using backslashes). unc+rest is always the input path. |
| 131 | Paths containing drive letters never have an UNC part. |
| 132 | """ |
| 133 | if p[1:2] == ':': |
| 134 | return '', p # Drive letter present |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 135 | firstTwo = p[0:2] |
| 136 | if firstTwo == '//' or firstTwo == '\\\\': |
| 137 | # is a UNC path: |
| 138 | # vvvvvvvvvvvvvvvvvvvv equivalent to drive letter |
| 139 | # \\machine\mountpoint\directories... |
| 140 | # directory ^^^^^^^^^^^^^^^ |
| 141 | normp = normcase(p) |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 142 | index = normp.find('\\', 2) |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 143 | if index == -1: |
| 144 | ##raise RuntimeError, 'illegal UNC path: "' + p + '"' |
| 145 | return ("", p) |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 146 | index = normp.find('\\', index + 1) |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 147 | if index == -1: |
| 148 | index = len(p) |
| 149 | return p[:index], p[index:] |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 150 | return '', p |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 151 | |
| 152 | |
| 153 | # Split a path in head (everything up to the last '/') and tail (the |
Guido van Rossum | 8f0fa9e | 1999-03-19 21:05:12 +0000 | [diff] [blame] | 154 | # rest). After the trailing '/' is stripped, the invariant |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 155 | # join(head, tail) == p holds. |
| 156 | # The resulting head won't end in '/' unless it is the root. |
| 157 | |
| 158 | def split(p): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 159 | """Split a pathname. |
| 160 | |
| 161 | Return tuple (head, tail) where tail is everything after the final slash. |
| 162 | Either part may be empty.""" |
Guido van Rossum | 8f0fa9e | 1999-03-19 21:05:12 +0000 | [diff] [blame] | 163 | |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 164 | d, p = splitdrive(p) |
Guido van Rossum | 8f0fa9e | 1999-03-19 21:05:12 +0000 | [diff] [blame] | 165 | # set i to index beyond p's last slash |
| 166 | i = len(p) |
| 167 | while i and p[i-1] not in '/\\': |
| 168 | i = i - 1 |
| 169 | head, tail = p[:i], p[i:] # now tail has no slashes |
| 170 | # remove trailing slashes from head, unless it's all slashes |
| 171 | head2 = head |
| 172 | while head2 and head2[-1] in '/\\': |
| 173 | head2 = head2[:-1] |
| 174 | head = head2 or head |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 175 | return d + head, tail |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 176 | |
| 177 | |
| 178 | # Split a path in root and extension. |
Guido van Rossum | 73e122f | 1997-01-22 00:17:26 +0000 | [diff] [blame] | 179 | # 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] | 180 | # pathname component; the root is everything before that. |
| 181 | # It is always true that root + ext == p. |
| 182 | |
| 183 | def splitext(p): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 184 | """Split the extension from a pathname. |
| 185 | |
| 186 | Extension is everything from the last dot to the end. |
| 187 | Return (root, ext), either part may be empty.""" |
Martin v. Löwis | de33379 | 2002-12-12 20:30:20 +0000 | [diff] [blame] | 188 | |
| 189 | i = p.rfind('.') |
| 190 | if i<=max(p.rfind('/'), p.rfind('\\')): |
| 191 | return p, '' |
| 192 | else: |
| 193 | return p[:i], p[i:] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 194 | |
| 195 | |
| 196 | # Return the tail (basename) part of a path. |
| 197 | |
| 198 | def basename(p): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 199 | """Returns the final component of a pathname""" |
| 200 | return split(p)[1] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 201 | |
| 202 | |
| 203 | # Return the head (dirname) part of a path. |
| 204 | |
| 205 | def dirname(p): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 206 | """Returns the directory component of a pathname""" |
| 207 | return split(p)[0] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 208 | |
| 209 | |
| 210 | # Return the longest prefix of all list elements. |
| 211 | |
| 212 | def commonprefix(m): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 213 | "Given a list of pathnames, returns the longest common leading component" |
| 214 | if not m: return '' |
Skip Montanaro | 6235831 | 2000-08-22 13:01:53 +0000 | [diff] [blame] | 215 | prefix = m[0] |
| 216 | for item in m: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 217 | for i in range(len(prefix)): |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 218 | if prefix[:i+1] != item[:i+1]: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 219 | prefix = prefix[:i] |
| 220 | if i == 0: return '' |
| 221 | break |
Skip Montanaro | 6235831 | 2000-08-22 13:01:53 +0000 | [diff] [blame] | 222 | return prefix |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 223 | |
| 224 | |
Guido van Rossum | 2bc1f8f | 1998-07-24 20:49:26 +0000 | [diff] [blame] | 225 | # Get size, mtime, atime of files. |
| 226 | |
| 227 | def getsize(filename): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 228 | """Return the size of a file, reported by os.stat()""" |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 229 | return os.stat(filename).st_size |
Guido van Rossum | 2bc1f8f | 1998-07-24 20:49:26 +0000 | [diff] [blame] | 230 | |
| 231 | def getmtime(filename): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 232 | """Return the last modification time of a file, reported by os.stat()""" |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 233 | return os.stat(filename).st_mtime |
Guido van Rossum | 2bc1f8f | 1998-07-24 20:49:26 +0000 | [diff] [blame] | 234 | |
| 235 | def getatime(filename): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 236 | """Return the last access time of a file, reported by os.stat()""" |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 237 | return os.stat(filename).st_atime |
Guido van Rossum | 2bc1f8f | 1998-07-24 20:49:26 +0000 | [diff] [blame] | 238 | |
Martin v. Löwis | 96a60e4 | 2002-12-31 13:11:54 +0000 | [diff] [blame] | 239 | def getctime(filename): |
| 240 | """Return the creation time of a file, reported by os.stat().""" |
| 241 | return os.stat(filename).st_ctime |
Guido van Rossum | 2bc1f8f | 1998-07-24 20:49:26 +0000 | [diff] [blame] | 242 | |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 243 | # Is a path a symbolic link? |
| 244 | # This will always return false on systems where posix.lstat doesn't exist. |
| 245 | |
| 246 | def islink(path): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 247 | """Test for symbolic link. On WindowsNT/95 always returns false""" |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 248 | return False |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 249 | |
| 250 | |
| 251 | # Does a path exist? |
| 252 | # This is false for dangling symbolic links. |
| 253 | |
| 254 | def exists(path): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 255 | """Test whether a path exists""" |
| 256 | try: |
| 257 | st = os.stat(path) |
| 258 | except os.error: |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 259 | return False |
| 260 | return True |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 261 | |
| 262 | |
| 263 | # Is a path a dos directory? |
| 264 | # This follows symbolic links, so both islink() and isdir() can be true |
| 265 | # for the same path. |
| 266 | |
| 267 | def isdir(path): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 268 | """Test whether a path is a directory""" |
| 269 | try: |
| 270 | st = os.stat(path) |
| 271 | except os.error: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 272 | return False |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 273 | return stat.S_ISDIR(st.st_mode) |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 274 | |
| 275 | |
| 276 | # Is a path a regular file? |
| 277 | # This follows symbolic links, so both islink() and isdir() can be true |
| 278 | # for the same path. |
| 279 | |
| 280 | def isfile(path): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 281 | """Test whether a path is a regular file""" |
| 282 | try: |
| 283 | st = os.stat(path) |
| 284 | except os.error: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 285 | return False |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 286 | return stat.S_ISREG(st.st_mode) |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 287 | |
| 288 | |
Guido van Rossum | f3c695c | 1999-04-06 19:32:19 +0000 | [diff] [blame] | 289 | # Is a path a mount point? Either a root (with or without drive letter) |
| 290 | # or an UNC path with at most a / or \ after the mount point. |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 291 | |
| 292 | def ismount(path): |
Guido van Rossum | ca99c2c | 1998-01-19 22:25:59 +0000 | [diff] [blame] | 293 | """Test whether a path is a mount point (defined as root of drive)""" |
Guido van Rossum | f3c695c | 1999-04-06 19:32:19 +0000 | [diff] [blame] | 294 | unc, rest = splitunc(path) |
| 295 | if unc: |
| 296 | return rest in ("", "/", "\\") |
Guido van Rossum | ca99c2c | 1998-01-19 22:25:59 +0000 | [diff] [blame] | 297 | p = splitdrive(path)[1] |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 298 | return len(p) == 1 and p[0] in '/\\' |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 299 | |
| 300 | |
| 301 | # Directory tree walk. |
| 302 | # For each directory under top (including top itself, but excluding |
| 303 | # '.' and '..'), func(arg, dirname, filenames) is called, where |
| 304 | # dirname is the name of the directory and filenames is the list |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 305 | # of files (and subdirectories etc.) in the directory. |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 306 | # The func may modify the filenames list, to implement a filter, |
| 307 | # or to impose a different order of visiting. |
| 308 | |
| 309 | def walk(top, func, arg): |
Tim Peters | cf5e6a4 | 2001-10-10 04:16:20 +0000 | [diff] [blame] | 310 | """Directory tree walk with callback function. |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 311 | |
Tim Peters | cf5e6a4 | 2001-10-10 04:16:20 +0000 | [diff] [blame] | 312 | For each directory in the directory tree rooted at top (including top |
| 313 | itself, but excluding '.' and '..'), call func(arg, dirname, fnames). |
| 314 | dirname is the name of the directory, and fnames a list of the names of |
| 315 | the files and subdirectories in dirname (excluding '.' and '..'). func |
| 316 | may modify the fnames list in-place (e.g. via del or slice assignment), |
| 317 | and walk will only recurse into the subdirectories whose names remain in |
| 318 | fnames; this can be used to implement a filter, or to impose a specific |
| 319 | order of visiting. No semantics are defined for, or required of, arg, |
| 320 | beyond that arg is always passed to func. It can be used, e.g., to pass |
| 321 | a filename pattern, or a mutable object designed to accumulate |
| 322 | statistics. Passing None for arg is common.""" |
| 323 | |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 324 | try: |
| 325 | names = os.listdir(top) |
| 326 | except os.error: |
| 327 | return |
| 328 | func(arg, top, names) |
| 329 | exceptions = ('.', '..') |
| 330 | for name in names: |
| 331 | if name not in exceptions: |
| 332 | name = join(top, name) |
| 333 | if isdir(name): |
| 334 | walk(name, func, arg) |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 335 | |
| 336 | |
| 337 | # Expand paths beginning with '~' or '~user'. |
| 338 | # '~' means $HOME; '~user' means that user's home directory. |
| 339 | # If the path doesn't begin with '~', or if the user or $HOME is unknown, |
| 340 | # the path is returned unchanged (leaving error reporting to whatever |
| 341 | # function is called with the expanded path as argument). |
| 342 | # See also module 'glob' for expansion of *, ? and [...] in pathnames. |
| 343 | # (A function should also be defined to do full *sh-style environment |
| 344 | # variable expansion.) |
| 345 | |
| 346 | def expanduser(path): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 347 | """Expand ~ and ~user constructs. |
| 348 | |
| 349 | If user or $HOME is unknown, do nothing.""" |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 350 | if path[:1] != '~': |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 351 | return path |
| 352 | i, n = 1, len(path) |
| 353 | while i < n and path[i] not in '/\\': |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 354 | i = i + 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 355 | if i == 1: |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 356 | if 'HOME' in os.environ: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 357 | userhome = os.environ['HOME'] |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 358 | elif not 'HOMEPATH' in os.environ: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 359 | return path |
| 360 | else: |
| 361 | try: |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 362 | drive = os.environ['HOMEDRIVE'] |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 363 | except KeyError: |
| 364 | drive = '' |
| 365 | userhome = join(drive, os.environ['HOMEPATH']) |
| 366 | else: |
| 367 | return path |
| 368 | return userhome + path[i:] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 369 | |
| 370 | |
| 371 | # Expand paths containing shell variable substitutions. |
| 372 | # The following rules apply: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 373 | # - no expansion within single quotes |
| 374 | # - no escape character, except for '$$' which is translated into '$' |
| 375 | # - ${varname} is accepted. |
| 376 | # - varnames can be made out of letters, digits and the character '_' |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 377 | # XXX With COMMAND.COM you can use any characters in a variable name, |
| 378 | # XXX except '^|<>='. |
| 379 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 380 | def expandvars(path): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 381 | """Expand shell variables of form $var and ${var}. |
| 382 | |
| 383 | Unknown variables are left unchanged.""" |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 384 | if '$' not in path: |
| 385 | return path |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 386 | import string |
Fred Drake | 79e75e1 | 2001-07-20 19:05:50 +0000 | [diff] [blame] | 387 | varchars = string.ascii_letters + string.digits + '_-' |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 388 | res = '' |
| 389 | index = 0 |
| 390 | pathlen = len(path) |
| 391 | while index < pathlen: |
| 392 | c = path[index] |
| 393 | if c == '\'': # no expansion within single quotes |
| 394 | path = path[index + 1:] |
| 395 | pathlen = len(path) |
| 396 | try: |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 397 | index = path.index('\'') |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 398 | res = res + '\'' + path[:index + 1] |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 399 | except ValueError: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 400 | res = res + path |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 401 | index = pathlen - 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 402 | elif c == '$': # variable or '$$' |
| 403 | if path[index + 1:index + 2] == '$': |
| 404 | res = res + c |
| 405 | index = index + 1 |
| 406 | elif path[index + 1:index + 2] == '{': |
| 407 | path = path[index+2:] |
| 408 | pathlen = len(path) |
| 409 | try: |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 410 | index = path.index('}') |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 411 | var = path[:index] |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 412 | if var in os.environ: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 413 | res = res + os.environ[var] |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 414 | except ValueError: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 415 | res = res + path |
| 416 | index = pathlen - 1 |
| 417 | else: |
| 418 | var = '' |
| 419 | index = index + 1 |
| 420 | c = path[index:index + 1] |
| 421 | while c != '' and c in varchars: |
| 422 | var = var + c |
| 423 | index = index + 1 |
| 424 | c = path[index:index + 1] |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 425 | if var in os.environ: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 426 | res = res + os.environ[var] |
| 427 | if c != '': |
| 428 | res = res + c |
| 429 | else: |
| 430 | res = res + c |
| 431 | index = index + 1 |
| 432 | return res |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 433 | |
| 434 | |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 435 | # 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] | 436 | # Previously, this function also truncated pathnames to 8+3 format, |
| 437 | # but as this module is called "ntpath", that's obviously wrong! |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 438 | |
| 439 | def normpath(path): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 440 | """Normalize path, eliminating double slashes, etc.""" |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 441 | path = path.replace("/", "\\") |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 442 | prefix, path = splitdrive(path) |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 443 | while path[:1] == "\\": |
| 444 | prefix = prefix + "\\" |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 445 | path = path[1:] |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 446 | comps = path.split("\\") |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 447 | i = 0 |
| 448 | while i < len(comps): |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 449 | if comps[i] in ('.', ''): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 450 | del comps[i] |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 451 | elif comps[i] == '..': |
| 452 | if i > 0 and comps[i-1] != '..': |
| 453 | del comps[i-1:i+1] |
| 454 | i -= 1 |
| 455 | elif i == 0 and prefix.endswith("\\"): |
| 456 | del comps[i] |
| 457 | else: |
| 458 | i += 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 459 | else: |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 460 | i += 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 461 | # If the path is now empty, substitute '.' |
| 462 | if not prefix and not comps: |
| 463 | comps.append('.') |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 464 | return prefix + "\\".join(comps) |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 465 | |
| 466 | |
| 467 | # Return an absolute path. |
| 468 | def abspath(path): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 469 | """Return the absolute version of a path""" |
Mark Hammond | f717f05 | 2002-01-17 00:44:26 +0000 | [diff] [blame] | 470 | try: |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 471 | from nt import _getfullpathname |
Mark Hammond | f717f05 | 2002-01-17 00:44:26 +0000 | [diff] [blame] | 472 | except ImportError: # Not running on Windows - mock up something sensible. |
| 473 | global abspath |
| 474 | def _abspath(path): |
| 475 | if not isabs(path): |
| 476 | path = join(os.getcwd(), path) |
| 477 | return normpath(path) |
| 478 | abspath = _abspath |
| 479 | return _abspath(path) |
| 480 | |
| 481 | if path: # Empty path must return current working directory. |
Mark Hammond | 647d2fe | 2000-08-14 06:20:32 +0000 | [diff] [blame] | 482 | try: |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 483 | path = _getfullpathname(path) |
| 484 | except WindowsError: |
Fred Drake | da05e97 | 2001-05-15 15:23:01 +0000 | [diff] [blame] | 485 | pass # Bad path - return unchanged. |
Mark Hammond | 647d2fe | 2000-08-14 06:20:32 +0000 | [diff] [blame] | 486 | else: |
| 487 | path = os.getcwd() |
Guido van Rossum | 6dfc792 | 1999-11-30 15:00:00 +0000 | [diff] [blame] | 488 | return normpath(path) |
Guido van Rossum | 83eeef4 | 2001-09-17 15:16:09 +0000 | [diff] [blame] | 489 | |
| 490 | # realpath is a no-op on systems without islink support |
| 491 | realpath = abspath |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 492 | # Win9x family and earlier have no Unicode filename support. |
Tim Peters | 26bc25a | 2002-10-09 07:56:04 +0000 | [diff] [blame] | 493 | supports_unicode_filenames = (hasattr(sys, "getwindowsversion") and |
| 494 | sys.getwindowsversion()[3] >= 2) |