8 examples of 'python os walk' in Python

Every line of 'python os walk' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
328def _walk_files():
329 # Check for a shortcut when running the tests interactively.
330 # If a BOTOCORE_TEST env var is defined, that file is used as the
331 # only test to run. Useful when doing feature development.
332 single_file = os.environ.get('BOTOCORE_TEST')
333 if single_file is not None:
334 yield os.path.abspath(single_file)
335 else:
336 for root, _, filenames in os.walk(TEST_DIR):
337 for filename in filenames:
338 yield os.path.join(root, filename)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

468def walk_files(dir: str, hidden: bool=False) -> Iterable[str]:
469 """Iterator over paths to non-directory files in dir.
470
471 The returned paths will all start with dir. In particular, if dir
472 is absolute, then all returned paths will be absolute.
473
474 If hidden=True, include hidden files and files inside hidden
475 directories."""
476 for root, dirs, files in os.walk(dir):
477 if not hidden:
478 del_hidden(dirs)
479 del_hidden(files)
480 for f in files:
481 yield os.path.join(root, f)
370def walk_path(path, exclude, ignorelinks, callback, *kargs, **kwargs):
371 """
372 Walk a directory and execute a callback function for each file found.
373 If path to a single file is given, the callback is excuted for this file.
374 The callback is also executed and counted for an empty directory.
375 :return: int with the number of files walked
376 """
377 count = 0
378
379 if os.path.isfile(path):
380 return execute_walk_callback(count, path, callback, *kargs, **kwargs)
381
382 os.chdir(path)
383 for root, dirs, files in os.walk('.', topdown=True, followlinks=True):
384 if not exclude_path(root, exclude):
385 count = execute_walk_callback(count, root,
386 callback, *kargs, **kwargs)
387
388 if os.path.islink(root) and ignorelinks:
389 break
390
391 for fname in files:
392 f = os.path.join(root, fname)
393 if not exclude_path(f, exclude):
394 count = execute_walk_callback(count, f,
395 callback, *kargs, **kwargs)
396 return count
721def walk(self, path):
722 """
723 Walk through a given directory and return all subdirectories and
724 subfiles in a format parsed for transfer. This traverses the path in a
725 top-down pattern.
726
727 :param str path: The directory to be traversed through.
728 :return: A list of :py:class:`DirectoryContents` instances representing the contents.
729 :rtype: list
730 """
731 contents = []
732 path = self.get_abspath(path)
733 for walker in os.walk(path):
734 contents.append(DirectoryContents(*walker))
735 return contents
31def _get_files(path):
32 for root, dirs, names in os.walk(path):
33 for name in names:
34 yield os.path.join(root, name)
26def walklevel(some_dir, level=1):
27 some_dir = some_dir.rstrip(os.path.sep)
28 assert os.path.isdir(some_dir)
29 num_sep = some_dir.count(os.path.sep)
30 for root, dirs, files in os.walk(some_dir):
31 yield root, dirs, files
32 num_sep_this = root.count(os.path.sep)
33 if num_sep + level <= num_sep_this:
34 del dirs[:]
2007def walk(self, top, topdown=True, onerror=None, followlinks=False):
2008 """Performs an os.walk operation over the fake filesystem.
2009
2010 Args:
2011 top: root directory from which to begin walk
2012 topdown: determines whether to return the tuples with the root as the
2013 first entry (True) or as the last, after all the child directory
2014 tuples (False)
2015 onerror: if not None, function which will be called to handle the
2016 os.error instance provided when os.listdir() fails
2017
2018 Yields:
2019 (path, directories, nondirectories) for top and each of its
2020 subdirectories. See the documentation for the builtin os module for
2021 further details.
2022 """
2023 top = self.path.normpath(top)
2024 if not followlinks and self.path.islink(top):
2025 return
2026 try:
2027 top_contents = self._ClassifyDirectoryContents(top)
2028 except OSError as e:
2029 top_contents = None
2030 if onerror is not None:
2031 onerror(e)
2032
2033 if top_contents is not None:
2034 if topdown:
2035 yield top_contents
2036
2037 for directory in top_contents[1]:
2038 if not followlinks and self.path.islink(directory):
2039 continue
2040 for contents in self.walk(self.path.join(top, directory),
2041 topdown=topdown, onerror=onerror, followlinks=followlinks):
2042 yield contents
2043
2044 if not topdown:
2045 yield top_contents
46def walk(basefd, path):
47 print '{', path
48 dirfd = openat(basefd, path, 0)
49 if dirfd < 0:
50 # error in openat()
51 return
52 dir = fdopendir(dirfd)
53 dirent = DIRENT()
54 result = DIRENT_p()
55 while True:
56 if readdir_r(dir, dirent, result):
57 # error in readdir_r()
58 break
59 if not result:
60 break
61 name = dirent.d_name
62 print '%3d %s' % (dirent.d_type, name)
63 if dirent.d_type == 4 and name != '.' and name != '..':
64 walk(dirfd, name)
65 closedir(dir)
66 print '}'

Related snippets