Library / SDK
Tinche/aiofiles avatar
Tinche/aiofiles

aiofiles moves blocking disk IO off the event loop without changing the file API

File support for asyncio

3,273 stars188 forksPythonApache-2.0

At a glance

What is it?
Tinche/aiofiles wraps open, tempfile and thirty file-related os functions as coroutines that hand each call to a thread pool executor. Nothing about the file object changes shape, but sixteen of its methods become awaitable and everything else stays exactly as blocking as before.
Who is it for?
aiofiles fits programs that already run on asyncio and only need file access to stop stalling the loop, with zero runtime dependencies and a Python floor of 3.9. It does not help sync code, it does not make disk IO genuinely non blocking, and it does not make every file method awaitable, so check the sixteen coroutine names before reaching for a method you assume is covered.
Can I use it commercially?
Yes. Apache-2.0 is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 29 days ago.
What is it written in?
Mainly Python, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on November 22, 2026, and from our analysis. They are not legal advice.

From daily snapshots

Star history

  • Tinche/aiofiles3,273 ★ · +10
Star history
Repository2026-09-282026-11-22
Tinche/aiofiles3.3k3.3k

10 Stars in the last 55 days, measured from the daily GitHub snapshot. Compare with other projects

Editorial analysis

aiofiles does not make disk IO async, it relocates it

The premise is stated in the opening line: ordinary local file IO is blocking, and cannot easily and portably be made asynchronous. aiofiles therefore does not pretend to solve that. What it introduces is asynchronous versions of files that support delegating operations to a separate thread pool. The blocking call still happens, still takes the same time and still hits the same disk, but the event loop is free while it runs, which matters because doing file IO may interfere with asyncio applications, which shouldn't block the executing thread. That distinction decides when the library is worth reaching for and when it is not. If the work is a handful of small reads on a path that is already cached, the thread hand-off costs more than the read. If it is a large file walked line by line while other coroutines need the loop, the hand-off is the whole point.

Sixteen method names are coroutines and the rest are not

The returned object has an API identical to an ordinary file, except that the following sixteen methods are coroutines and delegate to an executor: close, flush, isatty, read, readall, read1, readinto, readline, readlines, seek, seekable, tell, truncate, writable, write and writelines. That list is the whole contract, and its shape matters. Anything you call on the object that is not named there behaves like the blocking builtin it mirrors, so a script can look fully async and still stall the loop on an unlisted method. The read family is the one to check first: read, readall, read1, readinto, readline and readlines are all delegated, which covers iteration as well as single pulls. In case of failure, one of the usual exceptions will be raised, so error handling is unchanged from synchronous code.

python
async with aiofiles.open('filename', mode='r') as f:
    contents = await f.read()
print(contents)
'My file contents'

loop and executor are the only two knobs on aiofiles.open

Files are opened with the aiofiles.open() coroutine, which mirrors the builtin open and accepts two extra optional arguments. The first is loop. If loop is absent, the default loop will be used, as per the set asyncio policy, so you inherit whatever policy the host application installed rather than a private loop. The second is executor. If executor is not specified, the default event loop executor will be used. That default is the line worth pausing on: every read and write in your program shares one pool unless you pass something else, and a saturated pool turns a fast non blocking call into a queued one. Supplying executor per file is how you isolate a job that should not compete with the rest of the application. Neither argument appears in the plain open call, so code that migrates from builtin open to aiofiles.open is unchanged until you add them deliberately.

aiofiles.os names thirty functions from the os module

Beyond file objects, the aiofiles.os module contains executor-enabled coroutine versions of thirty useful os functions that deal with files. The bare names cover the filesystem operations: stat, statvfs, sendfile, rename, renames, replace, remove, unlink, mkdir, makedirs, rmdir, removedirs, link, symlink, readlink, listdir, scandir, access and getcwd. The path-prefixed half covers the queries that walk directories: path.abspath, path.exists, path.isfile, path.isdir, path.islink, path.ismount, path.getsize, path.getatime, path.getctime, path.samefile and path.sameopenfile. Every name keeps its original spelling and its original position in the os namespace, so porting an existing program is a matter of changing the import and awaiting the calls rather than renaming anything. Note what is absent: nothing here wraps the socket or process modules, and every entry is a filesystem call.

tempfile gets four wrappers and a context manager

aiofiles.tempfile implements TemporaryFile, NamedTemporaryFile, SpooledTemporaryFile and TemporaryDirectory. Results are returned wrapped with a context manager allowing use with async with and async for, which is the detail that makes them usable inside a coroutine: the same two keywords that drive a normal file also drive a temporary one.

python
async with aiofiles.tempfile.NamedTemporaryFile('wb+') as f:
    await f.write(b'Line1\n Line2')
    await f.seek(0)
    async for line in f:
        print(line)

async with aiofiles.tempfile.TemporaryDirectory() as d:
    filename = os.path.join(d, "file.ext")

The example opens the temporary file in read and write binary mode so that a seek back to zero and an async iteration over lines are both legal, and it joins a filename onto the directory object the context manager hands back. SpooledTemporaryFile matters more here than it does in synchronous code, since a file that rolls over to disk mid job is exactly the slow, blocking event the library exists to hide.

Standard streams come in text and bytes pairs

Six module level attributes give async access to the process streams: aiofiles.stdin, aiofiles.stdout and aiofiles.stderr for the text objects, and aiofiles.stdin_bytes, aiofiles.stdout_bytes and aiofiles.stderr_bytes for their corresponding .buffer properties. The pairing exists because text streams and binary streams are separate objects in Python, and code that patches or captures output usually reaches for one or the other. Anything that writes progress or diagnostics into a stream from inside a coroutine is a candidate for the text pair, while anything doing its own encoding belongs on the bytes pair. These are attributes rather than a function, so there is no await on the lookup itself: the await lands on the write or read call you make afterwards.

The version number is read out of git tags at build time

The manifest declares version as dynamic and configures the build backend to source it from version control: hatchling with hatch-vcs, and a raw-option setting local_scheme to no-local-version. No version string is stored in the project table, so an installed copy and the tag it was cut from are the only two places a number can be read. Recent tags are v25.1.0 on 2025-10-09, v24.1.0 on 2024-06-24 and v23.2.1 on 2023-08-11. Alongside that, the runtime declaration is unusually wide for a library this small: requires-python is >=3.9, and classifiers run from Python 3.9 through 3.14 on both CPython and PyPy, marked Production/Stable and OS Independent. dependencies is an empty list, which is the load-bearing detail here, since it means aiofiles adds no runtime packages to whatever asyncio application adopts it.

Mocking a file means registering the return type as well

The test story has two halves and both are required. Real file IO can be mocked by patching aiofiles.threadpool.sync_open as desired, which removes the disk from the test without touching your own code. The return type also needs to be registered with the aiofiles.threadpool.wrap dispatcher, so a mock object is mapped onto the right async base class. Skipping the registration step leaves a patch that intercepts the open call and then hands back something the wrapper cannot drive. Contributors are asked to run the suite with tox and to ensure the coverage at least stays the same before opening a pull request, and the repository carries a Justfile, uv.lock, tox.ini and a coverage configuration scoped to the aiofiles source package, so the same commands work locally and in CI.

Editorial conclusion

aiofiles fits programs that already run on asyncio and only need file access to stop stalling the loop, with zero runtime dependencies and a Python floor of 3.9. It does not help sync code, it does not make disk IO genuinely non blocking, and it does not make every file method awaitable, so check the sixteen coroutine names before reaching for a method you assume is covered. Verify that your default event loop executor is what you want, because that is the pool every unnamed call lands in.

Frequently asked questions

how to install aiofiles

The install command is pip install aiofiles. The manifest requires Python 3.9 or newer and declares dependencies as an empty list, with classifiers covering Python 3.9 through 3.14 on CPython and PyPy.

what is aiofiles

aiofiles is an Apache2 licensed library, written in Python, for handling local disk files in asyncio applications. It introduces asynchronous versions of files that support delegating operations to a separate thread pool.

how to use aiofiles

Open files with the aiofiles.open() coroutine, which mirrors the builtin open and accepts optional loop and executor arguments. Use async with aiofiles.open('filename', mode='r') as f and then await f.read(), or async for line in f for asynchronous iteration.

what does aiofiles do

It turns sixteen file methods into coroutines that delegate to an executor: close, flush, isatty, read, readall, read1, readinto, readline, readlines, seek, seekable, tell, truncate, writable, write and writelines. It also ships executor-enabled coroutine versions of thirty os functions and four tempfile interfaces.

aiofiles vs asyncio

aiofiles is an add-on layer rather than a separate async runtime. It declares dependencies as an empty list, so nothing is installed alongside it, and its entry points are coroutines built on the async and await constructs of PEP 492.

aiofiles vs open

aiofiles.open mirrors the builtin open and adds optional loop and executor arguments. The returned object behaves like an ordinary file except that close, flush, isatty, read, readall, read1, readinto, readline, readlines, seek, seekable, tell, truncate, writable, write and writelines are awaitable.

Official sources

  1. Issues
  2. License: Apache-2.0
  3. README
  4. Releases
  5. Tinche/aiofiles on GitHub
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/tinche-aiofiles.svg)](https://hysenlabs.com/projects/tinche-aiofiles)

Star history chart for your README

markdown
[![Star history](https://hysenlabs.com/chart/tinche-aiofiles.svg)](https://hysenlabs.com/projects/tinche-aiofiles)

Was this useful?