Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async os.walk attempted solution #167

Open
BeRT2me opened this issue Jun 10, 2023 · 1 comment
Open

Async os.walk attempted solution #167

BeRT2me opened this issue Jun 10, 2023 · 1 comment

Comments

@BeRT2me
Copy link

BeRT2me commented Jun 10, 2023

Async adapted version of os.walk, it'll only do "topdown" walking.

Feel free to add it in if it makes sense~

import os
import aiofiles.os

async def _walk(top, onerror, followlinks):
    dirs = []
    nondirs = []
    walk_dirs = []

    # We may not have read permission for top, in which case we can't
    # get a list of the files the directory contains.  os.walk
    # always suppressed the exception then, rather than blow up for a
    # minor reason when (say) a thousand readable directories are still
    # left to visit.  That logic is copied here.
    try:
        scandir_it = await aiofiles.os.scandir(top)
    except OSError as error:
        if onerror is not None:
            onerror(error)
        return

    with scandir_it:
        while True:
            try:
                try:
                    entry = next(scandir_it)
                except StopIteration:
                    break
            except OSError as error:
                if onerror is not None:
                    onerror(error)
                return

            try:
                is_dir = entry.is_dir()
            except OSError:
                # If is_dir() raises an OSError, consider that the entry is not
                # a directory, same behaviour than os.path.isdir().
                is_dir = False

            if is_dir:
                dirs.append(entry.name)
            else:
                nondirs.append(entry.name)

    yield top, dirs, nondirs

    # Recurse into sub-directories
    islink, join = aiofiles.os.path.islink, os.path.join
    for dirname in dirs:
        new_path = join(top, dirname)

        if followlinks or not await islink(new_path):
            async for x in _walk(new_path, onerror, followlinks):
                yield x

async def walk(top, onerror=None, followlinks=False):
    async for top, dirs, nondirs in _walk(top, onerror, followlinks):
        yield top, dirs, nondirs

#160

Un-deleted, I thought there was a bug, but there wasn't 🙃

@BeRT2me BeRT2me closed this as not planned Won't fix, can't repro, duplicate, stale Jun 10, 2023
@BeRT2me BeRT2me changed the title Async os.walk attempted solution Async os.walk failed solution Jun 10, 2023
@BeRT2me BeRT2me changed the title Async os.walk failed solution Async os.walk attempted solution Jun 10, 2023
@BeRT2me BeRT2me reopened this Jun 10, 2023
@GodefroyClair
Copy link

walk_dirs is not used

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants