Tinche / aiofiles

File support for asyncio
Apache License 2.0
2.67k stars 149 forks source link

async implementation of pathlib #60

Open graingert opened 5 years ago

graingert commented 5 years ago

so we can do things like:

import asyncio

import aiostream
from aiofiles import pathlib

async def export_as_js(p: pathlib.Path) -> None:
    t = await p.read_text()
    await asyncio.gather(
        p.with_suffix('.js').write_text(f'export default `{t}`;'),
        p.unlink(),
    )

async def amain():
    await aiostream.map(pathlib.Path('.').glob('*/**.html'), export_as_js)

def main():
    asyncio.run(amain())
alexdelorenzo commented 3 years ago

Hi @graingert, I released aiopath, an async implementation of pathlib, that does just this. It uses some features from aiofiles, as well.

To illustrate using your example with aiopath:

import asyncio

from aiostream import stream
from aiopath import AsyncPath as Path

async def export_as_js(p: Path) -> None:
    t = await p.read_text()
    await asyncio.gather(
        p.with_suffix('.js').write_text(f'export default `{t}`;'),
        p.unlink(),
    )

async def amain():
    await stream.map(Path('.').glob('**/*.html'), export_as_js)

def main():
    asyncio.run(amain())

main()

Check out the README for more examples.

aiopath is licensed under the LGPLv3, and it's available on PyPI. You can install it like so: python3 -m pip install aiopath.