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