本文整理汇总了Python中os.DirEntry方法的典型用法代码示例。如果您正苦于以下问题:Python os.DirEntry方法的具体用法?Python os.DirEntry怎么用?Python os.DirEntry使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os
的用法示例。
在下文中一共展示了os.DirEntry方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: import os [as 别名]
# 或者: from os import DirEntry [as 别名]
def main(dirname: str, output_file: str, ip_file: str, report_type: str = 'tex'):
nmap_command = ''
start_date = ''
builder = create_report_builder(report_type)
parser = FlanXmlParser()
for entry in os.scandir(dirname): # type: os.DirEntry
if not (entry.is_file() and entry.name.endswith('.xml')):
continue
data = parser.read_xml_file(entry.path)
parser.parse(data)
nmap_command = parse_nmap_command(data['nmaprun']['@args'])
start_date = data['nmaprun']['@startstr']
with open(output_file, 'w+') as output, open(ip_file) as ip_source:
create_report(parser, builder, nmap_command, start_date, output, ip_source)
示例2: scantree
# 需要导入模块: import os [as 别名]
# 或者: from os import DirEntry [as 别名]
def scantree(path):
"""Recursively scan a directory tree
:param str path: path to scan
:rtype: os.DirEntry
:return: DirEntry via generator
"""
for entry in os.scandir(path):
yield entry
if entry.is_dir(follow_symlinks=False):
yield from scantree(entry.path)
示例3: scantree
# 需要导入模块: import os [as 别名]
# 或者: from os import DirEntry [as 别名]
def scantree(path):
# type: (str) -> os.DirEntry
"""Recursively scan a directory tree
:param str path: path to scan
:rtype: DirEntry
:return: DirEntry via generator
"""
for entry in scandir(path):
if entry.is_dir(follow_symlinks=True):
# due to python2 compat, cannot use yield from here
for t in scantree(entry.path):
yield t
else:
yield entry
示例4: scantree
# 需要导入模块: import os [as 别名]
# 或者: from os import DirEntry [as 别名]
def scantree(path):
"""Recursively scan a directory tree
:param str path: path to scan
:rtype: os.DirEntry
:return: DirEntry via generator
"""
for entry in os.scandir(path):
if entry.is_dir(follow_symlinks=False):
yield from scantree(entry.path)
else:
yield entry
示例5: path_completion
# 需要导入模块: import os [as 别名]
# 或者: from os import DirEntry [as 别名]
def path_completion(prefix: str = '') -> Tuple[List[str], List[str]]:
prefix = prefix.replace(r'\ ', ' ')
dirs, files = [], []
base = '.'
if prefix.endswith('/'):
base = prefix
elif '/' in prefix:
base = os.path.dirname(prefix)
src = os.path.expandvars(os.path.expanduser(base))
src_prefix = os.path.abspath(os.path.expandvars(os.path.expanduser(prefix))) if prefix else ''
try:
items: Iterable[os.DirEntry] = os.scandir(src)
except FileNotFoundError:
items = ()
for x in items:
abspath = os.path.abspath(x.path)
if prefix and not abspath.startswith(src_prefix):
continue
if prefix:
q = prefix + abspath[len(src_prefix):].lstrip(os.sep)
q = os.path.expandvars(os.path.expanduser(q))
else:
q = os.path.relpath(abspath)
if x.is_dir():
dirs.append(q.rstrip(os.sep) + os.sep)
else:
files.append(q)
return dirs, files
示例6: get_core_module_directories
# 需要导入模块: import os [as 别名]
# 或者: from os import DirEntry [as 别名]
def get_core_module_directories():
'''List core module directories (not including libraries) under python_modules.
Returns:
List(os.DirEntry): List of core module directories
'''
core_module_root_dir = os.path.join(git_repo_root(), 'python_modules')
module_directories = [
dir_
for dir_ in os.scandir(core_module_root_dir)
if dir_.is_dir() and not dir_.name.startswith('.')
]
return module_directories
示例7: get_library_module_directories
# 需要导入模块: import os [as 别名]
# 或者: from os import DirEntry [as 别名]
def get_library_module_directories():
'''List library module directories under python_modules/libraries.
Returns:
List(os.DirEntry): List of core module directories
'''
library_module_root_dir = os.path.join(git_repo_root(), 'python_modules', 'libraries')
library_directories = [
dir_
for dir_ in os.scandir(library_module_root_dir)
if dir_.is_dir() and not dir_.name.startswith('.')
]
return library_directories
示例8: get_library_module_directories
# 需要导入模块: import os [as 别名]
# 或者: from os import DirEntry [as 别名]
def get_library_module_directories():
'''List library module directories under python_modules/libraries.
Returns:
List(os.DirEntry): List of core module directories
'''
library_module_root_dir = os.path.join(git_repo_root(), 'python_modules', 'libraries')
library_directories = [
dir_.name
for dir_ in os.scandir(library_module_root_dir)
if dir_.is_dir() and not dir_.name.startswith('.')
]
return library_directories
示例9: walk_dir
# 需要导入模块: import os [as 别名]
# 或者: from os import DirEntry [as 别名]
def walk_dir(self, rel_path):
"""
Recursively list all files in a folder.
"""
entries: List[os.DirEntry] = list(os.scandir(rel_path))
files = [(os.path.join(os.getcwd(), f.path), f.path) for f in entries if f.is_file()] # (filepath, filename)
for f in entries:
if f.is_dir():
files += self.walk_dir(f.path)
return files
示例10: load_series_datas
# 需要导入模块: import os [as 别名]
# 或者: from os import DirEntry [as 别名]
def load_series_datas(dir_path, default_author, last_updated):
for dir_entry in os.scandir(dir_path): # type: os.DirEntry
is_dir = dir_entry.is_dir()
if is_dir and os.path.exists(os.path.join(dir_entry.path,
SERIES_FILENAME)):
is_updated = dir_entry.stat().st_mtime > last_updated
if is_updated:
yield from load_series_datas(dir_entry.path,
default_author,
last_updated)
if dir_entry.name == SERIES_FILENAME:
yield SeriesData(dir_entry, default_author, last_updated)
示例11: load_article_datas
# 需要导入模块: import os [as 别名]
# 或者: from os import DirEntry [as 别名]
def load_article_datas(dir_path, default_author, last_updated, series_data=None):
for dir_entry in os.scandir(dir_path): # type: os.DirEntry
is_dir = dir_entry.is_dir()
if is_dir and os.path.exists(os.path.join(dir_entry.path,
ARTICLE_FILENAME)):
yield from load_article_datas(dir_entry.path, default_author, last_updated, series_data)
continue
is_markdown = dir_entry.is_file() and dir_entry.name.endswith('.md')
is_updated = dir_entry.stat().st_mtime > last_updated
if is_updated and is_markdown and dir_entry.name != SERIES_FILENAME:
yield ArticleData(dir_entry, default_author, series_data)
示例12: __init__
# 需要导入模块: import os [as 别名]
# 或者: from os import DirEntry [as 别名]
def __init__(self, dir_entry: os.DirEntry):
self.file_path = dir_entry.path
self.file_name = dir_entry.name
self.is_dir = self.file_name in [ARTICLE_FILENAME, SERIES_FILENAME]
self.dir_path = os.path.dirname(self.file_path) \
if self.is_dir else None
self.dir_name = self.dir_path.rsplit(os.path.sep, 1)[1] \
if self.is_dir else None
self.last_updated = timestamp_to_datetime(dir_entry.stat().st_mtime,
tzlocal.get_localzone())
with open(self.file_path) as f:
data = frontmatter.load(f)
self.frontmatter = data.metadata
self.markdown = data.content
示例13: __init__
# 需要导入模块: import os [as 别名]
# 或者: from os import DirEntry [as 别名]
def __init__(self, paths):
super().__init__()
self.watches = set()
for p in paths:
p = os.path.expanduser(p)
logger.debug('watch '+p)
self.watches.add(Repo.observer.schedule(self, p))
for f in os.scandir(p):
isinstance(f, os.DirEntry)
self.on_found(f.is_dir, f.path)
示例14: _walk_and_delete_upper_files
# 需要导入模块: import os [as 别名]
# 或者: from os import DirEntry [as 别名]
def _walk_and_delete_upper_files(
chroot: Chroot, should_delete: Callable[[Path, Path], bool] = lambda root_path: True
) -> None:
"""
Delete files and directories from `root` that are in `upper` but not `base`.
Ignore files and directories for which `should_delete(root_path) == False`.
"""
# lightweight recursion: (dirpath, scandir iterator) at each
# level of nesting
#
# We scandir *chroot.upper*. This is where all the _changes_ are
# recorded; so we want it to appear empty (except for maybe a few
# empty directories that mirror directories in chroot.base).
#
# DO NOT edit chroot.upper directly: that gives undefined behavior.
# Delete from chroot.root.
stack: List[Tuple[Path, Iterator[os.DirEntry]]] = [
(chroot.upper, os.scandir(str(chroot.upper)))
]
while stack:
cur_dir, cur_scandir = stack[-1]
try:
entry: os.DirEntry = next(cur_scandir)
except StopIteration:
stack.pop()
if cur_dir != chroot.upper: # we're done
# Delete the directory itself, unless it's in base layer or
# we didn't delete its children.
relative_path = Path(cur_dir).relative_to(chroot.upper)
base_path = chroot.base / relative_path
root_path = chroot.root / relative_path
if not base_path.exists() and should_delete(root_path):
try:
root_path.rmdir()
except OSError as err:
if err.errno == errno.ENOTEMPTY:
pass
else:
raise
continue
relative_path = Path(entry.path).relative_to(chroot.upper)
root_path = chroot.root / relative_path
if entry.is_dir(follow_symlinks=False):
if root_path.is_mount():
# Don't follow mountpoints. /root/.local/share/virtualenvs
# is a mountpoint in dev mode, to cache installed Python
# modules.
continue
stack.append((Path(entry.path), os.scandir(entry.path)))
elif should_delete(root_path):
root_path.unlink()