aiofile

3.9.0 · active · verified Thu Apr 02

aiofile provides real asynchronous file operations for asyncio applications. It addresses the blocking nature of ordinary file I/O by delegating operations to a separate thread pool, ensuring that file operations do not block the asyncio event loop. The library is Apache2 licensed, currently at version 3.9.0, and maintains a stable development status.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to asynchronously write to and read from a file using `aiofile.async_open`, which provides a familiar file-like interface. It also shows asynchronous iteration for reading files line by line.

import asyncio
from aiofile import async_open

async def main():
    # Write to a file asynchronously
    async with async_open("hello.txt", mode="w+") as f:
        await f.write("Hello, aiofile!")
        await f.seek(0)
        content = await f.read()
        print(f"Read: {content}")

    # Read a file line by line asynchronously
    async with async_open("hello.txt", mode="r") as f:
        async for line in f:
            print(f"Line: {line.strip()}")

asyncio.run(main())

view raw JSON →