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

List/delete content of a directory #142

Open
santigandolfo opened this issue Sep 1, 2022 · 2 comments
Open

List/delete content of a directory #142

santigandolfo opened this issue Sep 1, 2022 · 2 comments

Comments

@santigandolfo
Copy link
Contributor

How would be the recommended way to list all the files in a directory and then delete them.
Using python os you can do it this way:

    def clear_directory(self, dir: str):
       for f in os.listdir(dir):
           os.remove(os.path.join(dir, f))

But I can't seem to find a way to do this with aiofiles.os

@Tinche
Copy link
Owner

Tinche commented Sep 1, 2022

Just take your clear_directory function and run it in a background thread:

from asyncio import get_running_loop

async def coroutine():
    await get_running_loop().run_in_executor(None, clear_directory)

This is what aiofiles would do for you anyway.

@santigandolfo
Copy link
Contributor Author

Great. I've created this PR #143 to add the listdir function (and also the scandir function) to the main repo. In the meantime I'll be trying your solution. Thanks!

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