📁 Async pathlib for Python
Project description
📁 Async pathlib for Python
aiopath is a complete implementation of pathlib from Python 3.4+ that is compatible with asyncio and the async/await syntax. All I/O performed is asynchronous and awaitable.
aiopath is extensively typed with Python type annotations. aiopath also takes advantage of libaio on Linux.
Usage
aiopath.Path has the same API as pathlib.Path, and aiopath.AsyncPurePath works the same way as pathlib.PurePath. The only difference is that with aiopath, methods that perform I/O are asynchronous and awaitable, and methods that returned iterators now return async generators.
To run the following examples with top-level await expressions, launch an asynchronous Python REPL using python3 -m asyncio.
Basic
All of pathlib.Path's methods that perform synchronous I/O are reimplemented as asynchronous methods.
import tempfile
from pathlib import Path
from aiopath import AsyncPath
with tempfile.NamedTemporaryFile() as temp:
path = Path(temp.name)
apath = AsyncPath(temp.name)
# check existence
## sync
assert path.exists()
## async
assert await apath.exists()
# check if file
## sync
assert path.is_file()
## async
assert await apath.is_file()
# touch
path.touch()
await apath.touch()
# read and write text
text = "example"
await apath.write_text(text)
assert text == await apath.read_text()
assert not path.exists()
assert not await apath.exists()
You can convert pathlib.Path objects to aiopath.Path objects, and vice versa:
from pathlib import Path
from aiopath import AsyncPath
home: Path = Path.home()
ahome: AsyncPath = AsyncPath(home)
path: Path = Path(ahome)
assert isinstance(home, Path)
assert isinstance(ahome, AsyncPath)
assert isinstance(path, Path)
Opening a file
You can get an asynchronous file-like object handle by using asynchronous context managers.
import tempfile
from aiopath import AsyncPath
text: str = 'example'
with tempfile.NamedTemporaryFile() as temp:
apath = AsyncPath(temp.name)
async with apath.open(mode='w') as afile:
await afile.write(text)
result: str = await apath.read_text()
assert result == text
Globbing
aiopath implements pathlib globbing using async I/O and async generators.
from typing import List
from aiopath import AsyncPath
home: AsyncPath = await AsyncPath.home()
async for path in home.glob('*'):
assert isinstance(path, AsyncPath)
print(path)
downloads: AsyncPath = home / 'Downloads'
if await downloads.exists():
# caution! this might take awhile
paths: List[AsyncPath] = \
[path async for path in downloads.glob('**/*')]
Installation
Dependencies
- A POSIX compliant OS, or Windows
- Python 3.7+
requirements.txt
PyPI
$ python3 -m pip install aiopath
GitHub
$ python3 -m pip install -r requirements.txt
$ python3 setup.py install
Linux
This library will take advantage of libaio, which is compatible with Linux 4.18 and up.
$ sudo apt install libaio1
Support
Want to support this project and other open-source projects like it?
License
See LICENSE. If you'd like to use this project with a different license, please get in touch.
Credit
See CREDIT.md.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aiopath-0.2.2.tar.gz.
File metadata
- Download URL: aiopath-0.2.2.tar.gz
- Upload date:
- Size: 12.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8a195ef7fc6f9970419bcce747835769947d37fcb44e2855a865fc8639e1751a
|
|
| MD5 |
97b375eaa3bdfbd3fb78e83ce9ddb967
|
|
| BLAKE2b-256 |
d78eda0b7b2a25de016c57f484a62846d7a13fc84b27ccc6871ab6d19dd345bb
|
File details
Details for the file aiopath-0.2.2-py2.py3-none-any.whl.
File metadata
- Download URL: aiopath-0.2.2-py2.py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e32f22b8e6f67bbc077428406439adcf8982b98ebb56c1657e3e561a0cc744c
|
|
| MD5 |
8b0fa9334a5ad44b8889dcdcaa91ea8d
|
|
| BLAKE2b-256 |
dc2bebe112ce003d6160bcce3d7f4ffae2dc6750116a1be31855420a24f80ee7
|