def iglob(*args, include_hidden=False, **kwargs):
"""
A :py:func:`glob.iglob` that **optionally** but **truly** excludes hidden
files (i.e. even on *Windows*).
:py:func:`glob._ishidden`, which is implicitly used by :py:func:`glob.glob`
and :py:func:`glob.iglob` always filters out *dot* files but does not mind
about file's *HIDDEN* attribute on Windows.
**CAUTION:** this function **is not** thread-safe as it installs a trap at
runtime (i.e. for :py:func:`glob._ishidden`). The ``glob`` module must not
be used concurrently to this function.
"""
orig_ishidden = _glob._ishidden
if include_hidden:
_glob._ishidden = lambda x: False
else:
# original glob._ishidden() only removes "dot" files
# on windows, files have a "hidden" attribute
_glob._ishidden = _ishidden
try:
yield from _glob.iglob(*args, **kwargs)
finally:
_glob._ishidden = orig_ishidden
评论列表
文章目录