aiopath: Async pathlib for Python

0.7.7 · active · verified Thu Apr 16

aiopath provides an asynchronous implementation of Python's standard `pathlib` module, enabling non-blocking file system operations within `asyncio`, `trio`, and other `async`/`await` compatible frameworks. It mirrors `pathlib`'s API, making it familiar for users who need to perform I/O-bound tasks concurrently without blocking the event loop. The library is currently at version 0.7.7 and maintains an active development, addressing compatibility with newer Python versions and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic asynchronous file operations using `AsyncPath`, including creating a directory, writing text to a file, reading its content, checking its properties, and cleaning up the created files and directories.

import asyncio
from aiopath import AsyncPath
from pathlib import Path
import os

async def main():
    # Ensure a directory for testing exists
    test_dir = AsyncPath("aiopath_test_dir")
    if await test_dir.exists():
        await test_dir.rmdir()
    await test_dir.mkdir()

    file_path = test_dir / "my_async_file.txt"
    content = "Hello, async world!\nThis is a test."

    # Write text asynchronously
    await file_path.write_text(content)
    print(f"Wrote to {file_path.name}:")

    # Read text asynchronously
    read_content = await file_path.read_text()
    print(f"Read from {file_path.name}:\n---\n{read_content}--- ")

    # Check if file exists and is a file
    print(f"Does {file_path.name} exist? {await file_path.exists()}")
    print(f"Is {file_path.name} a file? {await file_path.is_file()}")

    # Clean up
    await file_path.unlink()
    await test_dir.rmdir()
    print(f"Cleaned up {file_path.name} and {test_dir.name}.")

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

view raw JSON →