本文整理汇总了Python中os.fwalk方法的典型用法代码示例。如果您正苦于以下问题:Python os.fwalk方法的具体用法?Python os.fwalk怎么用?Python os.fwalk使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os
的用法示例。
在下文中一共展示了os.fwalk方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _compare_to_walk
# 需要导入模块: import os [as 别名]
# 或者: from os import fwalk [as 别名]
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)))
示例2: setup_app
# 需要导入模块: import os [as 别名]
# 或者: from os import fwalk [as 别名]
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
示例3: _find_newest_mtime
# 需要导入模块: import os [as 别名]
# 或者: from os import fwalk [as 别名]
def _find_newest_mtime(self, path):
mtime = 0
for _root, _dirs, files, rootfd in os.fwalk(path):
for f in files:
mtime = max(mtime, os.stat(f, dir_fd=rootfd).st_mtime)
return mtime
示例4: walk
# 需要导入模块: import os [as 别名]
# 或者: from os import fwalk [as 别名]
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)
示例5: test_yields_correct_dir_fd
# 需要导入模块: import os [as 别名]
# 或者: from os import fwalk [as 别名]
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))
示例6: test_fd_leak
# 需要导入模块: import os [as 别名]
# 或者: from os import fwalk [as 别名]
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)
示例7: walk
# 需要导入模块: import os [as 别名]
# 或者: from os import fwalk [as 别名]
def walk(self, top, **kwargs):
for root, dirs, files, root_fd in os.fwalk(top, **kwargs):
yield (root, dirs, files)
示例8: dir_tree
# 需要导入模块: import os [as 别名]
# 或者: from os import fwalk [as 别名]
def dir_tree(d='.'):
stack = list()
nprev = -1
g = my_graph()
# all = nx.DiGraph()
# TODO
for path, dirs, files, fds in os.fwalk(d):
(dir, base) = os.path.split(path)
dir = re.sub(r'^\.\/', '', dir)
path = re.sub(r'^\.\/', '', path)
path2 = path.split(os.sep)
# print(path, fds, len(path2))
if re.match(r'\.repo/', path) or len(path2) > level_limit:
# print("skip", path)
continue
if len(path2) > 1:
# g.add_edge(path2[-2] + str(), path2[-1])
if g.number_of_edges() > limit:
g.add_edge(dir, '...')
break
g.add_edge(dir, path)
#g.add_node(path, label=path2[-1], xlabel='<<font point-size="1">'+path+'</font>>')
g.add_node(path, label=path2[-1])
#g.add_node(path, label=path2[-1], xlabel=path)
print(g.number_of_edges())
return g
示例9: fwalk
# 需要导入模块: import os [as 别名]
# 或者: from os import fwalk [as 别名]
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)
示例10: fwalk
# 需要导入模块: import os [as 别名]
# 或者: from os import fwalk [as 别名]
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.
if not follow_symlinks:
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, isinstance(top, bytes),
topdown, onerror, follow_symlinks)
finally:
close(topfd)
示例11: fwalk
# 需要导入模块: import os [as 别名]
# 或者: from os import fwalk [as 别名]
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)