Directory tree generator. For each directory in the directory tree rooted at top (including top itself, but excluding '.' and '..'), yields a 3-tuple dirpath, dirnames, filenames dirpath is a string, the path to the directory. dirnames is a list of the names of th
(top, topdown=True, onerror=None, followlinks=False)
| 280 | __all__.extend(["makedirs", "removedirs", "renames"]) |
| 281 | |
| 282 | def walk(top, topdown=True, onerror=None, followlinks=False): |
| 283 | """Directory tree generator. |
| 284 | |
| 285 | For each directory in the directory tree rooted at top (including top |
| 286 | itself, but excluding '.' and '..'), yields a 3-tuple |
| 287 | |
| 288 | dirpath, dirnames, filenames |
| 289 | |
| 290 | dirpath is a string, the path to the directory. dirnames is a list of |
| 291 | the names of the subdirectories in dirpath (including symlinks to directories, |
| 292 | and excluding '.' and '..'). |
| 293 | filenames is a list of the names of the non-directory files in dirpath. |
| 294 | Note that the names in the lists are just names, with no path components. |
| 295 | To get a full path (which begins with top) to a file or directory in |
| 296 | dirpath, do os.path.join(dirpath, name). |
| 297 | |
| 298 | If optional arg 'topdown' is true or not specified, the triple for a |
| 299 | directory is generated before the triples for any of its subdirectories |
| 300 | (directories are generated top down). If topdown is false, the triple |
| 301 | for a directory is generated after the triples for all of its |
| 302 | subdirectories (directories are generated bottom up). |
| 303 | |
| 304 | When topdown is true, the caller can modify the dirnames list in-place |
| 305 | (e.g., via del or slice assignment), and walk will only recurse into the |
| 306 | subdirectories whose names remain in dirnames; this can be used to prune the |
| 307 | search, or to impose a specific order of visiting. Modifying dirnames when |
| 308 | topdown is false has no effect on the behavior of os.walk(), since the |
| 309 | directories in dirnames have already been generated by the time dirnames |
| 310 | itself is generated. No matter the value of topdown, the list of |
| 311 | subdirectories is retrieved before the tuples for the directory and its |
| 312 | subdirectories are generated. |
| 313 | |
| 314 | By default errors from the os.scandir() call are ignored. If |
| 315 | optional arg 'onerror' is specified, it should be a function; it |
| 316 | will be called with one argument, an OSError instance. It can |
| 317 | report the error to continue with the walk, or raise the exception |
| 318 | to abort the walk. Note that the filename is available as the |
| 319 | filename attribute of the exception object. |
| 320 | |
| 321 | By default, os.walk does not follow symbolic links to subdirectories on |
| 322 | systems that support them. In order to get this functionality, set the |
| 323 | optional argument 'followlinks' to true. |
| 324 | |
| 325 | Caution: if you pass a relative pathname for top, don't change the |
| 326 | current working directory between resumptions of walk. walk never |
| 327 | changes the current directory, and assumes that the client doesn't |
| 328 | either. |
| 329 | |
| 330 | Example: |
| 331 | |
| 332 | import os |
| 333 | from os.path import join, getsize |
| 334 | for root, dirs, files in os.walk('python/Lib/email'): |
| 335 | print(root, "consumes ") |
| 336 | print(sum(getsize(join(root, name)) for name in files), end=" ") |
| 337 | print("bytes in", len(files), "non-directory files") |
| 338 | if 'CVS' in dirs: |
| 339 | dirs.remove('CVS') # don't visit CVS directories |