本文整理汇总了Python中os.scandir方法的典型用法代码示例。如果您正苦于以下问题:Python os.scandir方法的具体用法?Python os.scandir怎么用?Python os.scandir使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os
的用法示例。
在下文中一共展示了os.scandir方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: traverse_files
# 需要导入模块: import os [as 别名]
# 或者: from os import scandir [as 别名]
def traverse_files(path):
"""
遍历
:param path:
:return:
"""
for item in os.scandir(path):
file_name = get_file_name(item.path)[2]
# 判断是文件夹还是文件
if item.is_dir():
# print(item.path)
# 删除build文件夹
if file_name == 'build':
remove_dir(item.path)
else:
traverse_files(item.path)
示例2: get_dir_size
# 需要导入模块: import os [as 别名]
# 或者: from os import scandir [as 别名]
def get_dir_size(start_path = '.'):
total_size = 0
if 'scandir' in dir(os):
# using fast 'os.scandir' method (new in version 3.5)
for entry in os.scandir(start_path):
if entry.is_dir(follow_symlinks = False):
total_size += get_dir_size(entry.path)
elif entry.is_file(follow_symlinks = False):
total_size += entry.stat().st_size
else:
# using slow, but compatible 'os.listdir' method
print (start_path)
for entry in os.listdir(start_path):
full_path = os.path.abspath(os.path.join(start_path, entry))
if os.path.isdir(full_path):
total_size += get_dir_size(full_path)
elif os.path.isfile(full_path):
total_size += os.path.getsize(full_path)
return total_size
############################################################
### main ()
############################################################
示例3: __init__
# 需要导入模块: import os [as 别名]
# 或者: from os import scandir [as 别名]
def __init__(
self,
img_dir,
box_dir,
n_anchor,
resolution=DEFAULT_RESOLUTION,
transform=DEFAULT_TRANSFORM,
):
super().__init__()
self.img_files = list(sorted(scandir(img_dir), key=lambda f: f.name))
self.box_files = list(sorted(scandir(box_dir), key=lambda f: f.name))
self.n_anchor = n_anchor
# sizes(heights) of anchors have been reduced from the original paper
# now they are like: [5, 7, 10, 14, 20, ...] (times sqrt(2) each time)
self.anchors = torch.tensor([5 * (2 ** (i / 2)) for i in range(n_anchor)])
self.resolution = resolution
self.grid_resolution = [i // 16 for i in resolution]
self.transform = transform
示例4: traverse
# 需要导入模块: import os [as 别名]
# 或者: from os import scandir [as 别名]
def traverse(path, ignore_files=None):
if not os.path.exists(path):
return
if ignore_files is None:
ignore_files = []
for item in scandir(path):
full_path = os.path.join(path, item.name)
spec = pathspec.PathSpec.from_lines(
pathspec.patterns.GitWildMatchPattern, ignore_files
)
if spec.match_file(full_path):
logger.debug("Ignoring %s", item)
continue
if item.is_dir():
for result in traverse(item.path, ignore_files):
yield os.path.join(item.name, result)
else:
yield item.name
示例5: data_reader
# 需要导入模块: import os [as 别名]
# 或者: from os import scandir [as 别名]
def data_reader(input_dir, shuffle=True):
"""Read images from input_dir then shuffle them
Args:
input_dir: string, path of input dir, e.g., /path/to/dir
Returns:
file_paths: list of strings
"""
file_paths = []
for img_file in scandir(input_dir):
if img_file.name.endswith('.jpg') and img_file.is_file():
file_paths.append(img_file.path)
if shuffle:
# Shuffle the ordering of all image files in order to guarantee
# random ordering of the images with respect to label in the
# saved TFRecord files. Make the randomization repeatable.
shuffled_index = list(range(len(file_paths)))
random.seed(12345)
random.shuffle(shuffled_index)
file_paths = [file_paths[i] for i in shuffled_index]
return file_paths
示例6: select_file
# 需要导入模块: import os [as 别名]
# 或者: from os import scandir [as 别名]
def select_file(path, ext=''):
file_list = []
with os.scandir(path) as ls:
for item in ls:
if item.is_file():
filename = str(item.name)
if ext == '':
file_list.append(filename)
interrogative = "Select a file: "
elif filename.endswith(ext):
file_list.append(filename)
interrogative = "Select "+ext+" file: "
i = 1
for file in file_list:
print("["+str(i)+"] "+file)
i += 1
selected = validate_selection(interrogative, file_list)
return selected
示例7: render_ebook_directory
# 需要导入模块: import os [as 别名]
# 或者: from os import scandir [as 别名]
def render_ebook_directory(fpath):
print("Invoking scandir")
items = os.scandir(fpath)
items = [
{
'name' : item.name,
'fullpath' : os.path.join(fpath, item.name),
'directory' : fpath,
'is_dir' : item.is_dir(),
'is_file' : item.is_file(),
'relp' : urllib.parse.urljoin("/epub-reader/", urllib.parse.quote(os.path.relpath(os.path.join(fpath, item.name), settings.EBOOK_STORAGE_DIR)))
}
for item in items
]
items.sort(key=lambda x: x['name'])
print("Query done. Rendering with %s items" % (len(items), ))
return render_template('book-reader/reader-dir.html',
fpath = fpath,
items = items,
)
示例8: _load
# 需要导入模块: import os [as 别名]
# 或者: from os import scandir [as 别名]
def _load(self, path):
corpus = audiomate.Corpus(path=path)
for directory, type_name in DIRECTORIES.items():
source_directory = os.path.join(path, directory)
if not os.path.isdir(source_directory):
continue
it = os.scandir(source_directory)
for entry in it:
if not entry.name.endswith('.wav'):
continue
file_path = os.path.join(source_directory, entry.name)
file_idx = entry.name[0:-4] # chop of .wav
utterance_idx = file_idx # every file is a separate utterance
corpus.new_file(file_path, track_idx=file_idx, copy_file=False)
utterance = corpus.new_utterance(utterance_idx, file_idx)
utterance.set_label_list(annotations.LabelList.create_single(type_name, idx=audiomate.corpus.LL_DOMAIN))
return corpus
示例9: _iterdir
# 需要导入模块: import os [as 别名]
# 或者: from os import scandir [as 别名]
def _iterdir(dirname, dironly):
if not dirname:
if isinstance(dirname, bytes):
dirname = bytes(os.curdir, 'ASCII')
else:
dirname = os.curdir
try:
it = scandir(dirname)
for entry in it:
try:
if not dironly or entry.is_dir():
yield entry.name
except OSError:
pass
except OSError:
return
# Recursively yields relative pathnames inside a literal directory.
示例10: walk_output_dir
# 需要导入模块: import os [as 别名]
# 或者: from os import scandir [as 别名]
def walk_output_dir(self, skip_recurse=None):
"""Generate a list of files on disk (even if not in exported files)
Don't recurse into directories in `skip_recurse`.
Generate paths with forward slashes on all platforms.
"""
# os.walk() handles symlinked directories as directories
# due to this, it's simpler (and faster) to recurse ourselves
if not os.path.exists(self.output):
return
if skip_recurse is None:
skip_recurse = []
to_visit = ['']
while to_visit:
base = to_visit.pop()
with os.scandir(f"{self.output}/{base}") as scan_it:
for entry in scan_it:
if entry.is_symlink() or entry.is_file(follow_symlinks=False):
yield f"{base}{entry.name}"
elif entry.is_dir():
path = f"{base}{entry.name}"
if path not in skip_recurse:
to_visit.append(f"{path}/")
示例11: hash_dirs
# 需要导入模块: import os [as 别名]
# 或者: from os import scandir [as 别名]
def hash_dirs(dirs: Tuple[str, str]) -> bytes:
def hash_dir(dirname, ext, paths):
with os.scandir(dirname) as it:
for entry in it:
if entry.is_file() and entry.name.endswith(ext):
paths.append(entry.path)
elif entry.is_dir():
hash_dir(entry.path, ext, paths)
paths = []
for dirname, ext in dirs:
hash_dir(dirname, ext, paths)
h = hashlib.sha1() # sha1 is the fastest one.
for path in sorted(paths):
with open(path, 'rb') as f:
h.update(f.read())
return h.digest()
示例12: files
# 需要导入模块: import os [as 别名]
# 或者: from os import scandir [as 别名]
def files(startFolder=None):
extensions = ( '.12d', '.cvg', '.dat', '.vwf', '.out', '.nam', '.inp1', '.inp2',
'.sta', '.equ', '.eig', '.stm', '.mtx', '.net', '.inp0', '.rin',
'.fcv', 'dummy' )
if not startFolder:
startFolder = os.getcwd()
for f in os.scandir(startFolder):
if f.is_dir(): # if folder
files(f.path)
elif f.is_file() and f.name.endswith(extensions):
try:
os.remove(f.path)
sys.__stdout__.write('Delelted: ' + f.path + '\n')
except:
sys.__stdout__.write(f.path + ': ' + sys.exc_info()[1][1] + '\n')
# Cleaup old result files
示例13: organize_junk
# 需要导入模块: import os [as 别名]
# 或者: from os import scandir [as 别名]
def organize_junk():
for entry in os.scandir():
if entry.is_dir():
continue
file_path = Path(entry)
file_format = file_path.suffix.lower()
if file_format in FILE_FORMATS:
directory_path = Path(FILE_FORMATS[file_format])
directory_path.mkdir(exist_ok=True)
file_path.rename(directory_path.joinpath(file_path))
try:
os.mkdir("OTHER-FILES")
except:
pass
for dir in os.scandir():
try:
if dir.is_dir():
os.rmdir(dir)
else:
os.rename(os.getcwd() + '/' + str(Path(dir)), os.getcwd() + '/OTHER-FILES/' + str(Path(dir)))
except:
pass
示例14: find_corresponding_video_file
# 需要导入模块: import os [as 别名]
# 或者: from os import scandir [as 别名]
def find_corresponding_video_file(file_name: str, root_path: str, relative_directory: str):
"""
This function will attempt to find nfo file's corresponding video file
"""
if not file_name.endswith('.nfo'):
return file_name
filename, _ = os.path.splitext(file_name)
for f in os.scandir(os.path.join(root_path, relative_directory)):
_f, _ext = os.path.splitext(f.name)
if _f == filename and _ext != '.nfo':
return f.name
# by default just return input file name since nothing is found
print('[WARNING] cannot find video file for {} in {}'.format(
file_name, os.path.join(root_path, relative_directory)
))
return file_name
示例15: walk
# 需要导入模块: import os [as 别名]
# 或者: from os import scandir [as 别名]
def walk(path, parent_dir=None):
import importlib
import os
for entry in os.scandir(path):
if entry.is_file() and entry.name.endswith(".py"):
filename, _ = os.path.splitext(entry.name)
is_init = filename == "__init__"
if parent_dir:
module = parent_dir if is_init else f"{parent_dir}.{filename}"
else:
if is_init:
continue
module = filename
importlib.reload(eval(module))
elif entry.is_dir() and not entry.name.startswith((".", "__")):
dirname = f"{parent_dir}.{entry.name}" if parent_dir else entry.name
walk(entry.path, parent_dir=dirname)