Package: aiofiles
aiofiles: file support for asyncioaiofilesis an Apache2 licensed library, written in Python, for handling local
disk files in asyncio applications.Ordinary local file IO is blocking, and cannot easily and portably be made
asynchronous. This means doing file IO may interfere with asyncio applications,
which shouldn't block the executing thread. aiofiles helps with this by
introducing asynchronous versions of files that support delegating operations to
a separate thread pool.asyncwithaiofiles.open('filename',mode='r')asf:contents=awaitf.read()print(contents)'My file contents'Asynchronous iteration is also supported.asyncwithaiofiles.open('filename')asf:asyncforlineinf:...Asynchronous interface to tempfile module.asyncwithaiofiles.tempfile.TemporaryFile('wb')asf:awaitf.write(b'Hello, World!')Featuresa file API very similar to Python's standard, blocking APIsupport for buffered and unbuffered binary files, and buffered text filessupport forasync/await(PEP 492) constructsasync interface to tempfile moduleInstallationTo install aiofiles, simply:$pipinstallaiofilesUsageFiles are opened using theaiofiles.open()coroutine, which in addition to
mirroring the builtinopenaccepts optionalloopandexecutorarguments. Ifloopis absent, the default loop will be used, as per the
set asyncio policy. Ifexecutoris not specified, the default event loop
executor will be used.In case of success, an asynchronous file object is returned with an
API identical to an ordinary file, except the following methods are coroutines
and delegate to an executor:closeflushisattyreadreadallread1readintoreadlinereadlinesseekseekabletelltruncatewritablewritewritelinesIn case of failure, one of the usual exceptions will be raised.aiofiles.stdin,aiofiles.stdout,aiofiles.stderr,aiofiles.stdin_bytes,aiofiles.stdout_bytes, andaiofiles.stderr_bytesprovide async access tosys.stdin,sys.stdout,sys.stderr, and their corresponding.bufferproperties.Theaiofiles.osmodule contains executor-enabled coroutine versions of
several usefulosfunctions that deal with files:statstatvfssendfilerenamerenamesreplaceremoveunlinkmkdirmakedirsrmdirremovedirslinksymlinkreadlinklistdirscandiraccesspath.existspath.isfilepath.isdirpath.islinkpath.ismountpath.getsizepath.getatimepath.getctimepath.samefilepath.sameopenfileTempfileaiofiles.tempfileimplements the following interfaces:TemporaryFileNamedTemporaryFileSpooledTemporaryFileTemporaryDirectoryResults return wrapped with a context manager allowing use with async with and async for.asyncwithaiofiles.tempfile.NamedTemporaryFile('wb+')asf:awaitf.write(b'Line1\nLine2')awaitf.seek(0)asyncforlineinf:print(line)asyncwithaiofiles.tempfile.TemporaryDirectory()asd:filename=os.path.join(d,"file.ext")Writing tests for aiofilesReal file IO can be mocked by patchingaiofiles.threadpool.sync_openas desired. The return type also needs to be registered with theaiofiles.threadpool.wrapdispatcher:aiofiles.threadpool.wrap.register(mock.MagicMock)(lambda*args,**kwargs:threadpool.AsyncBufferedIOBase(*args,**kwargs))asyncdeftest_stuff():data='data'mock_file=mock.MagicMock()withmock.patch('aiofiles.threadpool.sync_open',return_value=mock_file)asmock_open:asyncwithaiofiles.open('filename','w')asf:awaitf.write(data)mock_file.write.assert_called_once_with(data)History23.2.1 (2023-08-09)Importos.statvfsconditionally to fix importing on non-UNIX systems.#171#17223.2.0 (2023-08-09)aiofiles is now tested on Python 3.12 too.#166#168On Python 3.12,aiofiles.tempfile.NamedTemporaryFilenow accepts adelete_on_closeargument, just like the stdlib version.On Python 3.12,aiofiles.tempfile.NamedTemporaryFileno longer exposes adeleteattribute, just like the stdlib version.Addedaiofiles.os.statvfsandaiofiles.os.path.ismount.#162UsePDMinstead of Poetry.#16923.1.0 (2023-02-09)Addedaiofiles.os.access.#146Removedaiofiles.tempfile.temptypes.AsyncSpooledTemporaryFile.softspace.#151Addedaiofiles.stdin,aiofiles.stdin_bytes, and other stdio streams.#154Transition toasyncio.get_running_loop(vsasyncio.get_event_loop) internally.22.1.0 (2022-09-04)Addedaiofiles.os.path.islink.#126Addedaiofiles.os.readlink.#125Addedaiofiles.os.symlink.#124Addedaiofiles.os.unlink.#123Addedaiofiles.os.link.#121Addedaiofiles.os.renames.#120Addedaiofiles.os.{listdir, scandir}.#143Switched to CalVer.Dropped Python 3.6 support. If you require it, use version 0.8.0.aiofiles is now tested on Python 3.11.0.8.0 (2021-11-27)aiofiles is now tested on Python 3.10.Addedaiofiles.os.replace.#107Addedaiofiles.os.{makedirs, removedirs}.Addedaiofiles.os.path.{exists, isfile, isdir, getsize, getatime, getctime, samefile, sameopenfile}.#63Addedsuffix,prefix,dirargs toaiofiles.tempfile.TemporaryDirectory.#1160.7.0 (2021-05-17)Added theaiofiles.tempfilemodule for async temporary files.#56Switched to Poetry and GitHub actions.Dropped 3.5 support.0.6.0 (2020-10-27)aiofilesis now tested on ppc64le.Addednameandmodeproperties to async file objects.#82Fixed a DeprecationWarning internally.#75Python 3.9 support and tests.0.5.0 (2020-04-12)Python 3.8 support. Code base modernization (usingasync/awaitinstead ofasyncio.coroutine/yield from).Addedaiofiles.os.remove,aiofiles.os.rename,aiofiles.os.mkdir,aiofiles.os.rmdir.#620.4.0 (2018-08-11)Python 3.7 support.Removed Python 3.3/3.4 support. If you use these versions, stick to aiofiles 0.3.x.0.3.2 (2017-09-23)The LICENSE is now included in the sdist.#310.3.1 (2017-03-10)Introduced a changelog.aiofiles.os.sendfilewill now work if the standardosmodule contains asendfilefunction.ContributingContributions are very welcome. Tests can be run withtox, please ensure
the coverage at least stays the same before you submit a pull request.