Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 1 | """Common operations on Posix pathnames. |
| 2 | |
| 3 | Instead of importing this module directly, import os and refer to |
| 4 | this module as os.path. The "os.path" name is an alias for this |
| 5 | module on Posix systems; on other systems (e.g. Mac, Windows), |
| 6 | os.path provides the same operations in a manner specific to that |
| 7 | platform, and is an alias to another module (e.g. macpath, ntpath). |
| 8 | |
| 9 | Some of this can actually be useful on non-Posix systems too, e.g. |
| 10 | for manipulation of the pathname component of URLs. |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 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 | |
Skip Montanaro | c62c81e | 2001-02-12 02:00:42 +0000 | [diff] [blame] | 16 | __all__ = ["normcase","isabs","join","splitdrive","split","splitext", |
| 17 | "basename","dirname","commonprefix","getsize","getmtime", |
Martin v. Löwis | 96a60e4 | 2002-12-31 13:11:54 +0000 | [diff] [blame] | 18 | "getatime","getctime","islink","exists","isdir","isfile","ismount", |
Skip Montanaro | c62c81e | 2001-02-12 02:00:42 +0000 | [diff] [blame] | 19 | "walk","expanduser","expandvars","normpath","abspath", |
Neal Norwitz | 61cdac6 | 2003-01-03 18:01:57 +0000 | [diff] [blame] | 20 | "samefile","sameopenfile","samestat", |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 21 | "curdir","pardir","sep","pathsep","defpath","altsep","extsep", |
Neal Norwitz | 61cdac6 | 2003-01-03 18:01:57 +0000 | [diff] [blame] | 22 | "realpath","supports_unicode_filenames"] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 23 | |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 24 | # strings representing various path-related bits and pieces |
| 25 | curdir = '.' |
| 26 | pardir = '..' |
| 27 | extsep = '.' |
| 28 | sep = '/' |
| 29 | pathsep = ':' |
| 30 | defpath = ':/bin:/usr/bin' |
| 31 | altsep = None |
| 32 | |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 33 | # Normalize the case of a pathname. Trivial in Posix, string.lower on Mac. |
| 34 | # On MS-DOS this may also turn slashes into backslashes; however, other |
| 35 | # normalizations (such as optimizing '../' away) are not allowed |
| 36 | # (another function should be defined to do that). |
| 37 | |
| 38 | def normcase(s): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 39 | """Normalize case of pathname. Has no effect under Posix""" |
| 40 | return s |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 41 | |
| 42 | |
Jeremy Hylton | a05e293 | 2000-06-28 14:48:01 +0000 | [diff] [blame] | 43 | # Return whether a path is absolute. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 44 | # Trivial in Posix, harder on the Mac or MS-DOS. |
| 45 | |
| 46 | def isabs(s): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 47 | """Test whether a path is absolute""" |
Walter Dörwald | 77cdeaf | 2003-06-17 13:13:40 +0000 | [diff] [blame] | 48 | return s.startswith('/') |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 49 | |
| 50 | |
Barry Warsaw | 384d249 | 1997-02-18 21:53:25 +0000 | [diff] [blame] | 51 | # Join pathnames. |
| 52 | # Ignore the previous parts if a part is absolute. |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 53 | # 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] | 54 | |
Barry Warsaw | 384d249 | 1997-02-18 21:53:25 +0000 | [diff] [blame] | 55 | def join(a, *p): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 56 | """Join two or more pathname components, inserting '/' as needed""" |
| 57 | path = a |
| 58 | for b in p: |
Walter Dörwald | 77cdeaf | 2003-06-17 13:13:40 +0000 | [diff] [blame] | 59 | if b.startswith('/'): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 60 | path = b |
Walter Dörwald | 77cdeaf | 2003-06-17 13:13:40 +0000 | [diff] [blame] | 61 | elif path == '' or path.endswith('/'): |
| 62 | path += b |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 63 | else: |
Walter Dörwald | 77cdeaf | 2003-06-17 13:13:40 +0000 | [diff] [blame] | 64 | path += '/' + b |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 65 | return path |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 66 | |
| 67 | |
Guido van Rossum | 2684738 | 1992-03-31 18:54:35 +0000 | [diff] [blame] | 68 | # 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] | 69 | # rest). If the path ends in '/', tail will be empty. If there is no |
| 70 | # '/' in the path, head will be empty. |
| 71 | # Trailing '/'es are stripped from head unless it is the root. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 72 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 73 | def split(p): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 74 | """Split a pathname. Returns tuple "(head, tail)" where "tail" is |
Fred Drake | c0ab93e | 2000-09-28 16:22:52 +0000 | [diff] [blame] | 75 | everything after the final slash. Either part may be empty.""" |
Fred Drake | 22fb839 | 2000-09-28 15:04:39 +0000 | [diff] [blame] | 76 | i = p.rfind('/') + 1 |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 77 | head, tail = p[:i], p[i:] |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 78 | if head and head != '/'*len(head): |
Walter Dörwald | 77cdeaf | 2003-06-17 13:13:40 +0000 | [diff] [blame] | 79 | head = head.rstrip('/') |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 80 | return head, tail |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 81 | |
| 82 | |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 83 | # Split a path in root and extension. |
Guido van Rossum | 422869a | 1996-08-20 20:24:17 +0000 | [diff] [blame] | 84 | # 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] | 85 | # pathname component; the root is everything before that. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 86 | # It is always true that root + ext == p. |
| 87 | |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 88 | def splitext(p): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 89 | """Split the extension from a pathname. Extension is everything from the |
Fred Drake | c0ab93e | 2000-09-28 16:22:52 +0000 | [diff] [blame] | 90 | last dot to the end. Returns "(root, ext)", either part may be empty.""" |
Martin v. Löwis | de33379 | 2002-12-12 20:30:20 +0000 | [diff] [blame] | 91 | i = p.rfind('.') |
| 92 | if i<=p.rfind('/'): |
| 93 | return p, '' |
| 94 | else: |
| 95 | return p[:i], p[i:] |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 96 | |
| 97 | |
Guido van Rossum | 221df24 | 1995-08-07 20:17:55 +0000 | [diff] [blame] | 98 | # Split a pathname into a drive specification and the rest of the |
| 99 | # path. Useful on DOS/Windows/NT; on Unix, the drive is always empty. |
| 100 | |
| 101 | def splitdrive(p): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 102 | """Split a pathname into drive and path. On Posix, drive is always |
Fred Drake | c0ab93e | 2000-09-28 16:22:52 +0000 | [diff] [blame] | 103 | empty.""" |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 104 | return '', p |
Guido van Rossum | 221df24 | 1995-08-07 20:17:55 +0000 | [diff] [blame] | 105 | |
| 106 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 107 | # Return the tail (basename) part of a path. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 108 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 109 | def basename(p): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 110 | """Returns the final component of a pathname""" |
| 111 | return split(p)[1] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 112 | |
| 113 | |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 114 | # Return the head (dirname) part of a path. |
| 115 | |
| 116 | def dirname(p): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 117 | """Returns the directory component of a pathname""" |
| 118 | return split(p)[0] |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 119 | |
| 120 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 121 | # Return the longest prefix of all list elements. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 122 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 123 | def commonprefix(m): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 124 | "Given a list of pathnames, returns the longest common leading component" |
| 125 | if not m: return '' |
Skip Montanaro | 6235831 | 2000-08-22 13:01:53 +0000 | [diff] [blame] | 126 | prefix = m[0] |
| 127 | for item in m: |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 128 | for i in range(len(prefix)): |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 129 | if prefix[:i+1] != item[:i+1]: |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 130 | prefix = prefix[:i] |
Walter Dörwald | 77cdeaf | 2003-06-17 13:13:40 +0000 | [diff] [blame] | 131 | if i == 0: |
| 132 | return '' |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 133 | break |
Skip Montanaro | 6235831 | 2000-08-22 13:01:53 +0000 | [diff] [blame] | 134 | return prefix |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 135 | |
| 136 | |
Guido van Rossum | 2bc1f8f | 1998-07-24 20:49:26 +0000 | [diff] [blame] | 137 | # Get size, mtime, atime of files. |
| 138 | |
| 139 | def getsize(filename): |
| 140 | """Return the size of a file, reported by os.stat().""" |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 141 | return os.stat(filename).st_size |
Guido van Rossum | 2bc1f8f | 1998-07-24 20:49:26 +0000 | [diff] [blame] | 142 | |
| 143 | def getmtime(filename): |
| 144 | """Return the last modification time of a file, reported by os.stat().""" |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 145 | return os.stat(filename).st_mtime |
Guido van Rossum | 2bc1f8f | 1998-07-24 20:49:26 +0000 | [diff] [blame] | 146 | |
| 147 | def getatime(filename): |
| 148 | """Return the last access time of a file, reported by os.stat().""" |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 149 | return os.stat(filename).st_atime |
Guido van Rossum | 2bc1f8f | 1998-07-24 20:49:26 +0000 | [diff] [blame] | 150 | |
Martin v. Löwis | 96a60e4 | 2002-12-31 13:11:54 +0000 | [diff] [blame] | 151 | def getctime(filename): |
| 152 | """Return the creation time of a file, reported by os.stat().""" |
| 153 | return os.stat(filename).st_ctime |
Guido van Rossum | 2bc1f8f | 1998-07-24 20:49:26 +0000 | [diff] [blame] | 154 | |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 155 | # Is a path a symbolic link? |
Guido van Rossum | d3876d3 | 1996-07-23 03:47:28 +0000 | [diff] [blame] | 156 | # 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] | 157 | |
| 158 | def islink(path): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 159 | """Test whether a path is a symbolic link""" |
| 160 | try: |
| 161 | st = os.lstat(path) |
| 162 | except (os.error, AttributeError): |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 163 | return False |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 164 | return stat.S_ISLNK(st.st_mode) |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 165 | |
| 166 | |
| 167 | # Does a path exist? |
| 168 | # This is false for dangling symbolic links. |
| 169 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 170 | def exists(path): |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 171 | """Test whether a path exists. Returns False for broken symbolic links""" |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 172 | try: |
| 173 | st = os.stat(path) |
| 174 | except os.error: |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 175 | return False |
| 176 | return True |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 177 | |
| 178 | |
Guido van Rossum | d3876d3 | 1996-07-23 03:47:28 +0000 | [diff] [blame] | 179 | # Is a path a directory? |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 180 | # This follows symbolic links, so both islink() and isdir() can be true |
| 181 | # for the same path. |
| 182 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 183 | def isdir(path): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 184 | """Test whether a path is a directory""" |
| 185 | try: |
| 186 | st = os.stat(path) |
| 187 | except os.error: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 188 | return False |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 189 | return stat.S_ISDIR(st.st_mode) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 190 | |
| 191 | |
Guido van Rossum | 2684738 | 1992-03-31 18:54:35 +0000 | [diff] [blame] | 192 | # Is a path a regular file? |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 193 | # 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] | 194 | # for the same path. |
| 195 | |
| 196 | def isfile(path): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 197 | """Test whether a path is a regular file""" |
| 198 | try: |
| 199 | st = os.stat(path) |
| 200 | except os.error: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 201 | return False |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 202 | return stat.S_ISREG(st.st_mode) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 203 | |
| 204 | |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 205 | # Are two filenames really pointing to the same file? |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 206 | |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 207 | def samefile(f1, f2): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 208 | """Test whether two pathnames reference the same actual file""" |
| 209 | s1 = os.stat(f1) |
| 210 | s2 = os.stat(f2) |
| 211 | return samestat(s1, s2) |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 212 | |
| 213 | |
| 214 | # Are two open files really referencing the same file? |
| 215 | # (Not necessarily the same file descriptor!) |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 216 | |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 217 | def sameopenfile(fp1, fp2): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 218 | """Test whether two open file objects reference the same file""" |
| 219 | s1 = os.fstat(fp1) |
| 220 | s2 = os.fstat(fp2) |
| 221 | return samestat(s1, s2) |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 222 | |
| 223 | |
| 224 | # Are two stat buffers (obtained from stat, fstat or lstat) |
| 225 | # describing the same file? |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 226 | |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 227 | def samestat(s1, s2): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 228 | """Test whether two stat buffers reference the same file""" |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 229 | return s1.st_ino == s2.st_ino and \ |
| 230 | s1.st_dev == s2.st_dev |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 231 | |
| 232 | |
| 233 | # Is a path a mount point? |
Guido van Rossum | d3876d3 | 1996-07-23 03:47:28 +0000 | [diff] [blame] | 234 | # (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] | 235 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 236 | def ismount(path): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 237 | """Test whether a path is a mount point""" |
| 238 | try: |
| 239 | s1 = os.stat(path) |
| 240 | s2 = os.stat(join(path, '..')) |
| 241 | except os.error: |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 242 | return False # It doesn't exist -- so not a mount point :-) |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 243 | dev1 = s1.st_dev |
| 244 | dev2 = s2.st_dev |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 245 | if dev1 != dev2: |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 246 | return True # path/.. on a different device as path |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 247 | ino1 = s1.st_ino |
| 248 | ino2 = s2.st_ino |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 249 | if ino1 == ino2: |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 250 | return True # path/.. is the same i-node as path |
| 251 | return False |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 252 | |
| 253 | |
| 254 | # Directory tree walk. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 255 | # For each directory under top (including top itself, but excluding |
| 256 | # '.' and '..'), func(arg, dirname, filenames) is called, where |
| 257 | # 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] | 258 | # of files (and subdirectories etc.) in the directory. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 259 | # The func may modify the filenames list, to implement a filter, |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 260 | # or to impose a different order of visiting. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 261 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 262 | def walk(top, func, arg): |
Tim Peters | cf5e6a4 | 2001-10-10 04:16:20 +0000 | [diff] [blame] | 263 | """Directory tree walk with callback function. |
| 264 | |
| 265 | For each directory in the directory tree rooted at top (including top |
| 266 | itself, but excluding '.' and '..'), call func(arg, dirname, fnames). |
| 267 | dirname is the name of the directory, and fnames a list of the names of |
| 268 | the files and subdirectories in dirname (excluding '.' and '..'). func |
| 269 | may modify the fnames list in-place (e.g. via del or slice assignment), |
| 270 | and walk will only recurse into the subdirectories whose names remain in |
| 271 | fnames; this can be used to implement a filter, or to impose a specific |
| 272 | order of visiting. No semantics are defined for, or required of, arg, |
| 273 | beyond that arg is always passed to func. It can be used, e.g., to pass |
| 274 | a filename pattern, or a mutable object designed to accumulate |
| 275 | statistics. Passing None for arg is common.""" |
| 276 | |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 277 | try: |
| 278 | names = os.listdir(top) |
| 279 | except os.error: |
| 280 | return |
| 281 | func(arg, top, names) |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 282 | for name in names: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 283 | name = join(top, name) |
Guido van Rossum | a490d58 | 2001-04-16 18:12:04 +0000 | [diff] [blame] | 284 | try: |
| 285 | st = os.lstat(name) |
| 286 | except os.error: |
| 287 | continue |
Neal Norwitz | ec7cf13 | 2002-06-06 18:16:14 +0000 | [diff] [blame] | 288 | if stat.S_ISDIR(st.st_mode): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 289 | walk(name, func, arg) |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 290 | |
| 291 | |
| 292 | # Expand paths beginning with '~' or '~user'. |
| 293 | # '~' means $HOME; '~user' means that user's home directory. |
| 294 | # If the path doesn't begin with '~', or if the user or $HOME is unknown, |
| 295 | # the path is returned unchanged (leaving error reporting to whatever |
| 296 | # function is called with the expanded path as argument). |
| 297 | # See also module 'glob' for expansion of *, ? and [...] in pathnames. |
| 298 | # (A function should also be defined to do full *sh-style environment |
| 299 | # variable expansion.) |
| 300 | |
| 301 | def expanduser(path): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 302 | """Expand ~ and ~user constructions. If user or $HOME is unknown, |
Fred Drake | c0ab93e | 2000-09-28 16:22:52 +0000 | [diff] [blame] | 303 | do nothing.""" |
Walter Dörwald | 77cdeaf | 2003-06-17 13:13:40 +0000 | [diff] [blame] | 304 | if not path.startswith('~'): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 305 | return path |
Walter Dörwald | a9da5ae | 2003-06-19 10:21:14 +0000 | [diff] [blame] | 306 | i = path.find('/', 1) |
| 307 | if i < 0: |
| 308 | i = len(path) |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 309 | if i == 1: |
Walter Dörwald | a9da5ae | 2003-06-19 10:21:14 +0000 | [diff] [blame] | 310 | if 'HOME' not in os.environ: |
Neal Norwitz | 609ba81 | 2002-09-05 21:08:25 +0000 | [diff] [blame] | 311 | import pwd |
Walter Dörwald | 77cdeaf | 2003-06-17 13:13:40 +0000 | [diff] [blame] | 312 | userhome = pwd.getpwuid(os.getuid()).pw_dir |
Neal Norwitz | 609ba81 | 2002-09-05 21:08:25 +0000 | [diff] [blame] | 313 | else: |
| 314 | userhome = os.environ['HOME'] |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 315 | else: |
| 316 | import pwd |
| 317 | try: |
| 318 | pwent = pwd.getpwnam(path[1:i]) |
| 319 | except KeyError: |
| 320 | return path |
Walter Dörwald | 77cdeaf | 2003-06-17 13:13:40 +0000 | [diff] [blame] | 321 | userhome = pwent.pw_dir |
| 322 | if userhome.endswith('/'): |
| 323 | i += 1 |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 324 | return userhome + path[i:] |
Guido van Rossum | 4732ccf | 1992-08-09 13:54:50 +0000 | [diff] [blame] | 325 | |
| 326 | |
| 327 | # Expand paths containing shell variable substitutions. |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 328 | # This expands the forms $variable and ${variable} only. |
Jeremy Hylton | a05e293 | 2000-06-28 14:48:01 +0000 | [diff] [blame] | 329 | # Non-existent variables are left unchanged. |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 330 | |
| 331 | _varprog = None |
Guido van Rossum | 4732ccf | 1992-08-09 13:54:50 +0000 | [diff] [blame] | 332 | |
| 333 | def expandvars(path): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 334 | """Expand shell variables of form $var and ${var}. Unknown variables |
Fred Drake | c0ab93e | 2000-09-28 16:22:52 +0000 | [diff] [blame] | 335 | are left unchanged.""" |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 336 | global _varprog |
| 337 | if '$' not in path: |
| 338 | return path |
| 339 | if not _varprog: |
| 340 | import re |
| 341 | _varprog = re.compile(r'\$(\w+|\{[^}]*\})') |
| 342 | i = 0 |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 343 | while True: |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 344 | m = _varprog.search(path, i) |
| 345 | if not m: |
| 346 | break |
| 347 | i, j = m.span(0) |
| 348 | name = m.group(1) |
Walter Dörwald | 77cdeaf | 2003-06-17 13:13:40 +0000 | [diff] [blame] | 349 | if name.startswith('{') and name.endswith('}'): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 350 | name = name[1:-1] |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 351 | if name in os.environ: |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 352 | tail = path[j:] |
| 353 | path = path[:i] + os.environ[name] |
| 354 | i = len(path) |
Walter Dörwald | 77cdeaf | 2003-06-17 13:13:40 +0000 | [diff] [blame] | 355 | path += tail |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 356 | else: |
| 357 | i = j |
| 358 | return path |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 359 | |
| 360 | |
| 361 | # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B. |
| 362 | # It should be understood that this may change the meaning of the path |
| 363 | # if it contains symbolic links! |
| 364 | |
| 365 | def normpath(path): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 366 | """Normalize path, eliminating double slashes, etc.""" |
Skip Montanaro | 018dfae | 2000-07-19 17:09:51 +0000 | [diff] [blame] | 367 | if path == '': |
| 368 | return '.' |
Marc-André Lemburg | bf222c9 | 2001-01-29 11:29:44 +0000 | [diff] [blame] | 369 | initial_slashes = path.startswith('/') |
| 370 | # POSIX allows one or two initial slashes, but treats three or more |
| 371 | # as single slash. |
Tim Peters | 658cba6 | 2001-02-09 20:06:00 +0000 | [diff] [blame] | 372 | if (initial_slashes and |
Marc-André Lemburg | bf222c9 | 2001-01-29 11:29:44 +0000 | [diff] [blame] | 373 | path.startswith('//') and not path.startswith('///')): |
| 374 | initial_slashes = 2 |
Fred Drake | 22fb839 | 2000-09-28 15:04:39 +0000 | [diff] [blame] | 375 | comps = path.split('/') |
Skip Montanaro | 018dfae | 2000-07-19 17:09:51 +0000 | [diff] [blame] | 376 | new_comps = [] |
| 377 | for comp in comps: |
| 378 | if comp in ('', '.'): |
| 379 | continue |
Marc-André Lemburg | bf222c9 | 2001-01-29 11:29:44 +0000 | [diff] [blame] | 380 | if (comp != '..' or (not initial_slashes and not new_comps) or |
Skip Montanaro | 018dfae | 2000-07-19 17:09:51 +0000 | [diff] [blame] | 381 | (new_comps and new_comps[-1] == '..')): |
| 382 | new_comps.append(comp) |
| 383 | elif new_comps: |
| 384 | new_comps.pop() |
| 385 | comps = new_comps |
Fred Drake | 22fb839 | 2000-09-28 15:04:39 +0000 | [diff] [blame] | 386 | path = '/'.join(comps) |
Marc-André Lemburg | bf222c9 | 2001-01-29 11:29:44 +0000 | [diff] [blame] | 387 | if initial_slashes: |
| 388 | path = '/'*initial_slashes + path |
Skip Montanaro | 018dfae | 2000-07-19 17:09:51 +0000 | [diff] [blame] | 389 | return path or '.' |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 390 | |
| 391 | |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 392 | def abspath(path): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 393 | """Return an absolute path.""" |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 394 | if not isabs(path): |
| 395 | path = join(os.getcwd(), path) |
| 396 | return normpath(path) |
Guido van Rossum | 83eeef4 | 2001-09-17 15:16:09 +0000 | [diff] [blame] | 397 | |
| 398 | |
| 399 | # Return a canonical path (i.e. the absolute location of a file on the |
| 400 | # filesystem). |
| 401 | |
| 402 | def realpath(filename): |
| 403 | """Return the canonical path of the specified filename, eliminating any |
| 404 | symbolic links encountered in the path.""" |
| 405 | filename = abspath(filename) |
| 406 | |
| 407 | bits = ['/'] + filename.split('/')[1:] |
| 408 | for i in range(2, len(bits)+1): |
| 409 | component = join(*bits[0:i]) |
| 410 | if islink(component): |
| 411 | resolved = os.readlink(component) |
| 412 | (dir, file) = split(component) |
| 413 | resolved = normpath(join(dir, resolved)) |
| 414 | newpath = join(*([resolved] + bits[i:])) |
| 415 | return realpath(newpath) |
Tim Peters | b64bec3 | 2001-09-18 02:26:39 +0000 | [diff] [blame] | 416 | |
Guido van Rossum | 83eeef4 | 2001-09-17 15:16:09 +0000 | [diff] [blame] | 417 | return filename |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 418 | |
| 419 | supports_unicode_filenames = False |