我们从Python开源项目中,提取了以下28个代码示例,用于说明如何使用os.fwalk()。
def _compare_to_walk(self, walk_kwargs, fwalk_kwargs): """ compare with walk() results. """ walk_kwargs = walk_kwargs.copy() fwalk_kwargs = fwalk_kwargs.copy() for topdown, follow_symlinks in itertools.product((True, False), repeat=2): walk_kwargs.update(topdown=topdown, followlinks=follow_symlinks) fwalk_kwargs.update(topdown=topdown, follow_symlinks=follow_symlinks) expected = {} for root, dirs, files in os.walk(**walk_kwargs): expected[root] = (set(dirs), set(files)) for root, dirs, files, rootfd in os.fwalk(**fwalk_kwargs): self.assertIn(root, expected) self.assertEqual(expected[root], (set(dirs), set(files)))
def setup_app(loop=None, js_profiles_path=None): app = web.Application(loop=loop, middlewares=[error_middleware]) js_profiles = {} if js_profiles_path: root, _, files, _ = next(os.fwalk(js_profiles_path)) js_files = filter(lambda f: os.path.splitext(f)[1] == '.js', files) _, profile_name = os.path.split(root) log.debug('adding profile "{}"'.format(profile_name)) js_profiles[profile_name] = "" for f in js_files: code = open(os.path.join(root, f)).read() js_profiles[profile_name] += '{}\n'.format(code) app.on_shutdown.append(on_shutdown) c = Chrome(host=HOST, port=PORT) app['chrome-driver'] = c app['js-profiles'] = js_profiles setup_routes(app) return app
def test_yields_correct_dir_fd(self): # check returned file descriptors for topdown, follow_symlinks in itertools.product((True, False), repeat=2): args = support.TESTFN, topdown, None for root, dirs, files, rootfd in os.fwalk(*args, follow_symlinks=follow_symlinks): # check that the FD is valid os.fstat(rootfd) # redundant check os.stat(rootfd) # check that listdir() returns consistent information self.assertEqual(set(os.listdir(rootfd)), set(dirs) | set(files))
def test_fd_leak(self): # Since we're opening a lot of FDs, we must be careful to avoid leaks: # we both check that calling fwalk() a large number of times doesn't # yield EMFILE, and that the minimum allocated FD hasn't changed. minfd = os.dup(1) os.close(minfd) for i in range(256): for x in os.fwalk(support.TESTFN): pass newfd = os.dup(1) self.addCleanup(os.close, newfd) self.assertEqual(newfd, minfd)
def tearDown(self): # cleanup for root, dirs, files, rootfd in os.fwalk(support.TESTFN, topdown=False): for name in files: os.unlink(name, dir_fd=rootfd) for name in dirs: st = os.stat(name, dir_fd=rootfd, follow_symlinks=False) if stat.S_ISDIR(st.st_mode): os.rmdir(name, dir_fd=rootfd) else: os.unlink(name, dir_fd=rootfd) os.rmdir(support.TESTFN)
def walk(self, directory, topdown=True, follow_symlinks=False): walk_it = os.fwalk(directory, topdown=topdown, follow_symlinks=follow_symlinks) for root, dirs, files, root_fd in walk_it: yield (root, dirs, files)
def fwalk(top=".", topdown=True, onerror=None, *, follow_symlinks=False, dir_fd=None): """Directory tree generator. This behaves exactly like walk(), except that it yields a 4-tuple dirpath, dirnames, filenames, dirfd `dirpath`, `dirnames` and `filenames` are identical to walk() output, and `dirfd` is a file descriptor referring to the directory `dirpath`. The advantage of fwalk() over walk() is that it's safe against symlink races (when follow_symlinks is False). If dir_fd is not None, it should be a file descriptor open to a directory, and top should be relative; top will then be relative to that directory. (dir_fd is always supported for fwalk.) Caution: Since fwalk() yields file descriptors, those are only valid until the next iteration step, so you should dup() them if you want to keep them for a longer period. Example: import os for root, dirs, files, rootfd in os.fwalk('python/Lib/email'): print(root, "consumes", end="") print(sum([os.stat(name, dir_fd=rootfd).st_size for name in files]), end="") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories """ if not isinstance(top, int) or not hasattr(top, '__index__'): top = fspath(top) # Note: To guard against symlink races, we use the standard # lstat()/open()/fstat() trick. orig_st = stat(top, follow_symlinks=False, dir_fd=dir_fd) topfd = open(top, O_RDONLY, dir_fd=dir_fd) try: if (follow_symlinks or (st.S_ISDIR(orig_st.st_mode) and path.samestat(orig_st, stat(topfd)))): yield from _fwalk(topfd, top, topdown, onerror, follow_symlinks) finally: close(topfd)
def fwalk(top=".", topdown=True, onerror=None, *, follow_symlinks=False, dir_fd=None): """Directory tree generator. This behaves exactly like walk(), except that it yields a 4-tuple dirpath, dirnames, filenames, dirfd `dirpath`, `dirnames` and `filenames` are identical to walk() output, and `dirfd` is a file descriptor referring to the directory `dirpath`. The advantage of fwalk() over walk() is that it's safe against symlink races (when follow_symlinks is False). If dir_fd is not None, it should be a file descriptor open to a directory, and top should be relative; top will then be relative to that directory. (dir_fd is always supported for fwalk.) Caution: Since fwalk() yields file descriptors, those are only valid until the next iteration step, so you should dup() them if you want to keep them for a longer period. Example: import os for root, dirs, files, rootfd in os.fwalk('python/Lib/email'): print(root, "consumes", end="") print(sum([os.stat(name, dir_fd=rootfd).st_size for name in files]), end="") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories """ # Note: To guard against symlink races, we use the standard # lstat()/open()/fstat() trick. orig_st = stat(top, follow_symlinks=False, dir_fd=dir_fd) topfd = open(top, O_RDONLY, dir_fd=dir_fd) try: if (follow_symlinks or (st.S_ISDIR(orig_st.st_mode) and path.samestat(orig_st, stat(topfd)))): yield from _fwalk(topfd, top, topdown, onerror, follow_symlinks) finally: close(topfd)