MCPcopy Index your code
hub / github.com/python/cpython / walk

Function walk

Lib/os.py:308–446  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Callers 1

walk_treeFunction · 0.50

Calls 8

islinkFunction · 0.85
is_junctionMethod · 0.80
onerrorFunction · 0.70
joinFunction · 0.70
popMethod · 0.45
is_dirMethod · 0.45
appendMethod · 0.45
is_symlinkMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…