Typing Stubs for aiofiles

25.1.0.20251011 · active · verified Mon Apr 06

types-aiofiles provides type stubs for the aiofiles library, enabling static type checkers like MyPy and Pyright to verify code that uses aiofiles. It is part of the typeshed project and is regularly updated, with version 25.1.0.20251011 aiming for aiofiles==25.1.*.

Warnings

Install

Imports

Quickstart

This example demonstrates basic asynchronous file writing and reading using `aiofiles`. Ensure `aiofiles` is installed alongside `types-aiofiles` for runtime execution.

import asyncio
import aiofiles

async def main():
    # Write to a file asynchronously
    async with aiofiles.open('example.txt', mode='w') as f:
        await f.write('Hello, aiofiles!\n')
        await f.write('This is an async file operation.')
    print('Wrote to example.txt')

    # Read from a file asynchronously
    async with aiofiles.open('example.txt', mode='r') as f:
        contents = await f.read()
    print(f'Read from example.txt:\n{contents}')

    # Clean up (optional)
    import aiofiles.os
    await aiofiles.os.remove('example.txt')
    print('Cleaned up example.txt')

if __name__ == '__main__':
    asyncio.run(main())

view raw JSON →