当前位置: 首页>>代码示例>>Python>>正文


Python os.scandir方法代码示例

本文整理汇总了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) 
开发者ID:xingag,项目名称:tools_python,代码行数:19,代码来源:delete_build.py

示例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 ()
############################################################ 
开发者ID:jeonghoonkang,项目名称:BerePi,代码行数:25,代码来源:directorych.py

示例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 
开发者ID:zzzDavid,项目名称:ICDAR-2019-SROIE,代码行数:24,代码来源:_data.py

示例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 
开发者ID:MichaelAquilina,项目名称:S4,代码行数:22,代码来源:local.py

示例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 
开发者ID:vanhuyz,项目名称:CycleGAN-TensorFlow,代码行数:26,代码来源:build_data.py

示例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 
开发者ID:tonymorony,项目名称:komodo-cctools-python,代码行数:20,代码来源:tuilib.py

示例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,
		) 
开发者ID:fake-name,项目名称:ReadableWebProxy,代码行数:23,代码来源:ebook_view.py

示例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 
开发者ID:ynop,项目名称:audiomate,代码行数:26,代码来源:gtzan.py

示例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. 
开发者ID:danhper,项目名称:bigcode-tools,代码行数:20,代码来源:glob.py

示例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}/") 
开发者ID:CommunityDragon,项目名称:CDTB,代码行数:25,代码来源:export.py

示例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() 
开发者ID:edgedb,项目名称:edgedb,代码行数:21,代码来源:buildmeta.py

示例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 
开发者ID:calculix,项目名称:ccx2paraview,代码行数:20,代码来源:clean.py

示例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 
开发者ID:srcecde,项目名称:lazy-junk-organizer,代码行数:26,代码来源:orgjunk.py

示例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 
开发者ID:ddd354,项目名称:JAVOneStop,代码行数:20,代码来源:file_scanner.py

示例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) 
开发者ID:mrachinskiy,项目名称:commotion,代码行数:24,代码来源:__init__.py


注:本文中的os.scandir方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。