Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 1 | # Module 'riscospath' -- common operations on RISC OS pathnames. |
| 2 | |
| 3 | # contributed by Andrew Clover ( andrew@oaktree.co.uk ) |
| 4 | |
| 5 | # The "os.path" name is an alias for this module on RISC OS systems; |
| 6 | # on other systems (e.g. Mac, Windows), os.path provides the same |
| 7 | # operations in a manner specific to that platform, and is an alias |
| 8 | # to another module (e.g. macpath, ntpath). |
| 9 | |
| 10 | """ |
| 11 | Instead of importing this module directly, import os and refer to this module |
| 12 | as os.path. |
| 13 | """ |
| 14 | |
| 15 | |
| 16 | # Imports - make an error-generating swi object if the swi module is not |
| 17 | # available (ie. we are not running on RISC OS Python) |
| 18 | |
| 19 | import os, stat, string |
| 20 | |
| 21 | try: |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 22 | import swi |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 23 | except ImportError: |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 24 | class _swi: |
| 25 | def swi(*a): |
| 26 | raise AttributeError, 'This function only available under RISC OS' |
| 27 | block= swi |
| 28 | swi= _swi() |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 29 | |
| 30 | [_false, _true]= range(2) |
| 31 | |
| 32 | _roots= ['$', '&', '%', '@', '\\'] |
| 33 | |
| 34 | |
| 35 | # _allowMOSFSNames |
| 36 | # After importing riscospath, set _allowMOSFSNames true if you want the module |
| 37 | # to understand the "-SomeFS-" notation left over from the old BBC Master MOS, |
| 38 | # as well as the standard "SomeFS:" notation. Set this to be fully backwards |
| 39 | # compatible but remember that "-SomeFS-" can also be a perfectly valid file |
| 40 | # name so care must be taken when splitting and joining paths. |
| 41 | |
| 42 | _allowMOSFSNames= _false |
| 43 | |
| 44 | |
| 45 | ## Path manipulation, RISC OS stylee. |
| 46 | |
| 47 | def _split(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 48 | """ |
| 49 | split filing system name (including special field) and drive specifier from rest |
| 50 | of path. This is needed by many riscospath functions. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 51 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 52 | dash= _allowMOSFSNames and p[:1]=='-' |
| 53 | if dash: |
| 54 | q= string.find(p, '-', 1)+1 |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 55 | else: |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 56 | if p[:1]==':': |
| 57 | q= 0 |
| 58 | else: |
| 59 | q= string.find(p, ':')+1 # q= index of start of non-FS portion of path |
| 60 | s= string.find(p, '#') |
| 61 | if s==-1 or s>q: |
| 62 | s= q # find end of main FS name, not including special field |
| 63 | else: |
| 64 | for c in p[dash:s]: |
Fred Drake | 79e75e1 | 2001-07-20 19:05:50 +0000 | [diff] [blame] | 65 | if c not in string.ascii_letters: |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 66 | q= 0 |
| 67 | break # disallow invalid non-special-field characters in FS name |
| 68 | r= q |
| 69 | if p[q:q+1]==':': |
| 70 | r= string.find(p, '.', q+1)+1 |
| 71 | if r==0: |
| 72 | r= len(p) # find end of drive name (if any) following FS name (if any) |
| 73 | return (p[:q], p[q:r], p[r:]) |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 74 | |
| 75 | |
| 76 | def normcase(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 77 | """ |
| 78 | Normalize the case of a pathname. This converts to lowercase as the native RISC |
| 79 | OS filesystems are case-insensitive. However, not all filesystems have to be, |
| 80 | and there's no simple way to find out what type an FS is argh. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 81 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 82 | return string.lower(p) |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 83 | |
| 84 | |
| 85 | def isabs(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 86 | """ |
| 87 | Return whether a path is absolute. Under RISC OS, a file system specifier does |
| 88 | not make a path absolute, but a drive name or number does, and so does using the |
| 89 | symbol for root, URD, library, CSD or PSD. This means it is perfectly possible |
| 90 | to have an "absolute" URL dependent on the current working directory, and |
| 91 | equally you can have a "relative" URL that's on a completely different device to |
| 92 | the current one argh. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 93 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 94 | (fs, drive, path)= _split(p) |
| 95 | return drive!='' or path[:1] in _roots |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 96 | |
| 97 | |
| 98 | def join(a, *p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 99 | """ |
| 100 | Join path elements with the directory separator, replacing the entire path when |
| 101 | an absolute or FS-changing path part is found. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 102 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 103 | j= a |
| 104 | for b in p: |
| 105 | (fs, drive, path)= _split(b) |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 106 | if j=='' or fs!='' or drive!='' or path[:1] in _roots: |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 107 | j= b |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 108 | elif j[-1]==':': |
| 109 | j= j+b |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 110 | else: |
| 111 | j= j+'.'+b |
| 112 | return j |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 113 | |
| 114 | |
| 115 | def split(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 116 | """ |
| 117 | Split a path in head (everything up to the last '.') and tail (the rest). FS |
| 118 | name must still be dealt with separately since special field may contain '.'. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 119 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 120 | (fs, drive, path)= _split(p) |
| 121 | q= string.rfind(path, '.') |
| 122 | if q!=-1: |
| 123 | return (fs+drive+path[:q], path[q+1:]) |
| 124 | return ('', p) |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 125 | |
| 126 | |
| 127 | def splitext(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 128 | """ |
| 129 | Split a path in root and extension. This assumes the 'using slash for dot and |
| 130 | dot for slash with foreign files' convention common in RISC OS is in force. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 131 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 132 | (tail, head)= split(p) |
| 133 | if '/' in head: |
| 134 | q= len(head)-string.rfind(head, '/') |
| 135 | return (p[:-q], p[-q:]) |
| 136 | return (p, '') |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 137 | |
| 138 | |
| 139 | def splitdrive(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 140 | """ |
| 141 | Split a pathname into a drive specification (including FS name) and the rest of |
| 142 | the path. The terminating dot of the drive name is included in the drive |
| 143 | specification. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 144 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 145 | (fs, drive, path)= _split(p) |
| 146 | return (fs+drive, p) |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 147 | |
| 148 | |
| 149 | def basename(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 150 | """ |
| 151 | Return the tail (basename) part of a path. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 152 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 153 | return split(p)[1] |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 154 | |
| 155 | |
| 156 | def dirname(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 157 | """ |
| 158 | Return the head (dirname) part of a path. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 159 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 160 | return split(p)[0] |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 161 | |
| 162 | |
| 163 | def commonprefix(ps): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 164 | """ |
| 165 | Return the longest prefix of all list elements. Purely string-based; does not |
| 166 | separate any path parts. Why am I in os.path? |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 167 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 168 | if len(ps)==0: |
| 169 | return '' |
| 170 | prefix= ps[0] |
| 171 | for p in ps[1:]: |
| 172 | prefix= prefix[:len(p)] |
| 173 | for i in range(len(prefix)): |
| 174 | if prefix[i] <> p[i]: |
| 175 | prefix= prefix[:i] |
| 176 | if i==0: |
| 177 | return '' |
| 178 | break |
| 179 | return prefix |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 180 | |
| 181 | |
| 182 | ## File access functions. Why are we in os.path? |
| 183 | |
| 184 | def getsize(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 185 | """ |
| 186 | Return the size of a file, reported by os.stat(). |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 187 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 188 | st= os.stat(p) |
| 189 | return st[stat.ST_SIZE] |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 190 | |
| 191 | |
| 192 | def getmtime(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 193 | """ |
| 194 | Return the last modification time of a file, reported by os.stat(). |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 195 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 196 | st = os.stat(p) |
| 197 | return st[stat.ST_MTIME] |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 198 | |
| 199 | getatime= getmtime |
| 200 | |
| 201 | |
| 202 | # RISC OS-specific file access functions |
| 203 | |
| 204 | def exists(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 205 | """ |
| 206 | Test whether a path exists. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 207 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 208 | try: |
| 209 | return swi.swi('OS_File', '5s;i', p)!=0 |
| 210 | except swi.error: |
| 211 | return 0 |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 212 | |
| 213 | |
| 214 | def isdir(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 215 | """ |
| 216 | Is a path a directory? Includes image files. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 217 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 218 | try: |
| 219 | return swi.swi('OS_File', '5s;i', p) in [2, 3] |
| 220 | except swi.error: |
| 221 | return 0 |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 222 | |
| 223 | |
| 224 | def isfile(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 225 | """ |
| 226 | Test whether a path is a file, including image files. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 227 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 228 | try: |
| 229 | return swi.swi('OS_File', '5s;i', p) in [1, 3] |
| 230 | except swi.error: |
| 231 | return 0 |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 232 | |
| 233 | |
| 234 | def islink(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 235 | """ |
| 236 | RISC OS has no links or mounts. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 237 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 238 | return _false |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 239 | |
| 240 | ismount= islink |
| 241 | |
| 242 | |
| 243 | # Same-file testing. |
| 244 | |
| 245 | # samefile works on filename comparison since there is no ST_DEV and ST_INO is |
| 246 | # not reliably unique (esp. directories). First it has to normalise the |
| 247 | # pathnames, which it can do 'properly' using OS_FSControl since samefile can |
| 248 | # assume it's running on RISC OS (unlike normpath). |
| 249 | |
| 250 | def samefile(fa, fb): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 251 | """ |
| 252 | Test whether two pathnames reference the same actual file. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 253 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 254 | l= 512 |
| 255 | b= swi.block(l) |
| 256 | swi.swi('OS_FSControl', 'isb..i', 37, fa, b, l) |
| 257 | fa= b.ctrlstring() |
| 258 | swi.swi('OS_FSControl', 'isb..i', 37, fb, b, l) |
| 259 | fb= b.ctrlstring() |
| 260 | return fa==fb |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 261 | |
| 262 | |
| 263 | def sameopenfile(a, b): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 264 | """ |
| 265 | Test whether two open file objects reference the same file. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 266 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 267 | return os.fstat(a)[stat.ST_INO]==os.fstat(b)[stat.ST_INO] |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 268 | |
| 269 | |
| 270 | ## Path canonicalisation |
| 271 | |
| 272 | # 'user directory' is taken as meaning the User Root Directory, which is in |
| 273 | # practice never used, for anything. |
| 274 | |
| 275 | def expanduser(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 276 | (fs, drive, path)= _split(p) |
| 277 | l= 512 |
| 278 | b= swi.block(l) |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 279 | |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 280 | if path[:1]!='@': |
| 281 | return p |
| 282 | if fs=='': |
| 283 | fsno= swi.swi('OS_Args', '00;i') |
| 284 | swi.swi('OS_FSControl', 'iibi', 33, fsno, b, l) |
| 285 | fsname= b.ctrlstring() |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 286 | else: |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 287 | if fs[:1]=='-': |
| 288 | fsname= fs[1:-1] |
| 289 | else: |
| 290 | fsname= fs[:-1] |
| 291 | fsname= string.split(fsname, '#', 1)[0] # remove special field from fs |
| 292 | x= swi.swi('OS_FSControl', 'ib2s.i;.....i', 54, b, fsname, l) |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 293 | if x<l: |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 294 | urd= b.tostring(0, l-x-1) |
| 295 | else: # no URD! try CSD |
| 296 | x= swi.swi('OS_FSControl', 'ib0s.i;.....i', 54, b, fsname, l) |
| 297 | if x<l: |
| 298 | urd= b.tostring(0, l-x-1) |
| 299 | else: # no CSD! use root |
| 300 | urd= '$' |
| 301 | return fsname+':'+urd+path[1:] |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 302 | |
| 303 | # Environment variables are in angle brackets. |
| 304 | |
| 305 | def expandvars(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 306 | """ |
| 307 | Expand environment variables using OS_GSTrans. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 308 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 309 | l= 512 |
| 310 | b= swi.block(l) |
| 311 | return b.tostring(0, swi.swi('OS_GSTrans', 'sbi;..i', p, b, l)) |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 312 | |
| 313 | |
| 314 | # Return an absolute path. |
| 315 | |
| 316 | def abspath(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 317 | return normpath(join(os.getcwd(), p)) |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 318 | |
| 319 | |
Guido van Rossum | 83eeef4 | 2001-09-17 15:16:09 +0000 | [diff] [blame] | 320 | # realpath is a no-op on systems without islink support |
| 321 | realpath = abspath |
| 322 | |
| 323 | |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 324 | # Normalize a path. Only special path element under RISC OS is "^" for "..". |
| 325 | |
| 326 | def normpath(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 327 | """ |
| 328 | Normalize path, eliminating up-directory ^s. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 329 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 330 | (fs, drive, path)= _split(p) |
| 331 | rhs= '' |
| 332 | ups= 0 |
| 333 | while path!='': |
| 334 | (path, el)= split(path) |
| 335 | if el=='^': |
| 336 | ups= ups+1 |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 337 | else: |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 338 | if ups>0: |
| 339 | ups= ups-1 |
| 340 | else: |
| 341 | if rhs=='': |
| 342 | rhs= el |
| 343 | else: |
| 344 | rhs= el+'.'+rhs |
| 345 | while ups>0: |
| 346 | ups= ups-1 |
| 347 | rhs= '^.'+rhs |
| 348 | return fs+drive+rhs |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 349 | |
| 350 | |
| 351 | # Directory tree walk. |
| 352 | # Independent of host system. Why am I in os.path? |
| 353 | |
| 354 | def walk(top, func, arg): |
Tim Peters | cf5e6a4 | 2001-10-10 04:16:20 +0000 | [diff] [blame] | 355 | """Directory tree walk with callback function. |
| 356 | |
| 357 | For each directory in the directory tree rooted at top (including top |
| 358 | itself, but excluding '.' and '..'), call func(arg, dirname, fnames). |
| 359 | dirname is the name of the directory, and fnames a list of the names of |
| 360 | the files and subdirectories in dirname (excluding '.' and '..'). func |
| 361 | may modify the fnames list in-place (e.g. via del or slice assignment), |
| 362 | and walk will only recurse into the subdirectories whose names remain in |
| 363 | fnames; this can be used to implement a filter, or to impose a specific |
| 364 | order of visiting. No semantics are defined for, or required of, arg, |
| 365 | beyond that arg is always passed to func. It can be used, e.g., to pass |
| 366 | a filename pattern, or a mutable object designed to accumulate |
| 367 | statistics. Passing None for arg is common.""" |
| 368 | |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 369 | try: |
| 370 | names= os.listdir(top) |
| 371 | except os.error: |
| 372 | return |
| 373 | func(arg, top, names) |
| 374 | for name in names: |
| 375 | name= join(top, name) |
| 376 | if isdir(name) and not islink(name): |
| 377 | walk(name, func, arg) |