Skip to content

Instantly share code, notes, and snippets.

@michaeltrainor
Last active August 1, 2020 14:10
Show Gist options
  • Save michaeltrainor/90c1ab7bc66c96edc42f6e200447138c to your computer and use it in GitHub Desktop.
Save michaeltrainor/90c1ab7bc66c96edc42f6e200447138c to your computer and use it in GitHub Desktop.
asynchronous file path generator
# standard libraries
import os
import asyncio
from pathlib import Path
async def get_files(search_path, type_filter=None):
""" asynchronous file path generator
"""
p = Path(search_path)
for i in p.iterdir():
if i.is_file():
if not type_filter or i.suffix in type_filter:
yield i
if i.is_dir():
async for f in get_files(str(i), type_filter):
yield f
if __name__ == "__main__":
async def foo():
return [i async for i in get_files("/var/log", ['.log'])]
loop = asyncio.get_event_loop()
try:
files = loop.run_until_complete(foo())
except StopAsyncIteration:
loop.close()
print(files)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment