or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

core-file-operations.mdindex.mdos-operations.mdpath-operations.mdtempfile-operations.md
tile.json

tessl/pypi-aiofiles

File support for asyncio applications providing async/await compatible file operations.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/aiofiles@24.1.x

To install, run

npx @tessl/cli install tessl/pypi-aiofiles@24.1.0

index.mddocs/

aiofiles

File support for asyncio applications providing async/await compatible file operations. aiofiles wraps standard Python file I/O operations and delegates them to a thread pool executor, preventing blocking of the asyncio event loop during file operations.

Package Information

  • Package Name: aiofiles
  • Language: Python
  • Installation: pip install aiofiles

Core Imports

import aiofiles

For temporary file operations:

import aiofiles.tempfile

For OS file system operations:

import aiofiles.os
from aiofiles import ospath

Basic Usage

import asyncio
import aiofiles

async def main():
    # Basic file reading
    async with aiofiles.open('example.txt', mode='r') as f:
        contents = await f.read()
        print(contents)
    
    # Basic file writing
    async with aiofiles.open('output.txt', mode='w') as f:
        await f.write('Hello, async world!')
    
    # Line-by-line reading with async iteration
    async with aiofiles.open('data.txt', mode='r') as f:
        async for line in f:
            print(line.strip())

asyncio.run(main())

Architecture

aiofiles uses a delegation pattern where all I/O operations are wrapped and executed in a thread pool executor:

  • AiofilesContextManager: Async context manager that handles file opening and closing
  • AsyncBase classes: Base wrappers that delegate operations to thread pool
  • File type wrappers: Specific async wrappers for different file types (text, binary, etc.)
  • Standard streams: Async access to stdin, stdout, stderr

This design ensures that file operations don't block the asyncio event loop while maintaining an API nearly identical to Python's built-in file operations.

Capabilities

Core File Operations

Basic file opening, reading, writing, and access to standard streams. These operations form the foundation of aiofiles functionality.

def open(
    file,
    mode: str = "r", 
    buffering: int = -1,
    encoding: str = None,
    errors: str = None,
    newline: str = None,
    closefd: bool = True,
    opener = None,
    *,
    loop = None,
    executor = None
) -> AiofilesContextManager: ...

# Standard stream objects
stdin: AsyncTextIndirectIOWrapper
stdout: AsyncTextIndirectIOWrapper  
stderr: AsyncTextIndirectIOWrapper
stdin_bytes: AsyncIndirectBufferedIOBase
stdout_bytes: AsyncIndirectBufferedIOBase
stderr_bytes: AsyncIndirectBufferedIOBase

Core File Operations

Temporary File Operations

Async interface to Python's tempfile module, providing temporary files and directories with automatic cleanup.

def NamedTemporaryFile(
    mode: str = "w+b",
    buffering: int = -1, 
    encoding: str = None,
    newline: str = None,
    suffix: str = None,
    prefix: str = None,
    dir: str = None,
    delete: bool = True,
    delete_on_close: bool = True,  # Python 3.12+ only
    *,
    loop = None,
    executor = None
) -> AiofilesContextManager: ...

def TemporaryFile(
    mode: str = "w+b",
    buffering: int = -1,
    encoding: str = None, 
    newline: str = None,
    suffix: str = None,
    prefix: str = None,
    dir: str = None,
    *,
    loop = None,
    executor = None
) -> AiofilesContextManager: ...

Temporary File Operations

OS File System Operations

Async versions of os module functions for file system operations like renaming, removing, creating directories, and getting file statistics.

async def stat(path: str, *, loop = None, executor = None): ...
async def rename(src: str, dst: str, *, loop = None, executor = None): ...
async def remove(path: str, *, loop = None, executor = None): ...
async def mkdir(path: str, mode: int = 0o777, *, loop = None, executor = None): ...
async def listdir(path: str = ".", *, loop = None, executor = None): ...

OS File System Operations

OS Path Operations

Async versions of os.path module functions for path manipulation and information gathering.

async def exists(path: str, *, loop = None, executor = None) -> bool: ...
async def isfile(path: str, *, loop = None, executor = None) -> bool: ...
async def isdir(path: str, *, loop = None, executor = None) -> bool: ...
async def getsize(path: str, *, loop = None, executor = None) -> int: ...
async def getmtime(path: str, *, loop = None, executor = None) -> float: ...

OS Path Operations

Types

class AiofilesContextManager:
    """Async context manager for aiofiles operations."""
    def __init__(self, coro): ...
    async def __aenter__(self): ...
    async def __aexit__(self, exc_type, exc_val, exc_tb): ...
    def __await__(self): ...

class AsyncBase:
    """Base class for async file wrappers."""
    def __init__(self, file, loop, executor): ...
    def __aiter__(self): ...
    async def __anext__(self): ...