aioshutil: Asynchronous shutil module

1.6 · active · verified Wed Apr 15

aioshutil is a Python library that provides asynchronous versions of functions from the standard `shutil` module. It achieves asynchronicity by running blocking I/O operations in a thread pool, thereby preventing the blocking of the asyncio event loop. The library is actively maintained, with the current version being 1.6.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `aioshutil` to perform common file operations like copying, moving, and removing directories asynchronously within an `asyncio` application. It mirrors the `shutil` API but with `await` calls.

import asyncio
import aioshutil
import os

async def main():
    # Ensure a directory exists for demonstration
    if not os.path.exists('test_dir_src'):
        os.makedirs('test_dir_src')
    with open('test_dir_src/test_file.txt', 'w') as f:
        f.write('Hello, aioshutil!')

    print('Copying file asynchronously...')
    await aioshutil.copy('test_dir_src/test_file.txt', 'test_dir_dest_copy.txt')
    print('File copied.')

    print('Moving file asynchronously...')
    await aioshutil.move('test_dir_src/test_file.txt', 'test_dir_dest_move.txt')
    print('File moved.')

    print('Removing directory asynchronously...')
    await aioshutil.rmtree('test_dir_src')
    print('Directory removed.')

    # Clean up created files/dirs if they exist from previous runs/failures
    if os.path.exists('test_dir_dest_copy.txt'):
        os.remove('test_dir_dest_copy.txt')
    if os.path.exists('test_dir_dest_move.txt'):
        os.remove('test_dir_dest_move.txt')

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

view raw JSON →