Pathlib support for asyncio
Project description
aiopathlib: Pathlib support for asyncio
aiopathlib is written in Python, for handling local disk files in asyncio applications.
Base on aiofiles and just like pathlib, but use await.
with open('filename', 'w') as fp:
fp.write('My file contents')
text = await aiopathlib.AsyncPath('filename').read_text()
print(text)
'My file contents'
content = await aiopathlib.AsyncPath(Path('filename')).read_bytes()
print(content)
b'My file contents'
Asynchronous interface to create folder.
from aiopathlib import AsyncPath
apath = AsyncPath('dirname/subpath')
if not await apath.exists():
await apath.mkdir(parents=True)
Features
- a file API very similar to Python's standard package
pathlib
, blocking API - support for buffered and unbuffered binary files, and buffered text files
- support for
async
/await
(:PEP:492
) constructs
Installation
To install aiopathlib, simply:
$ pip install aiopathlib
Usage
These functions are awaitable
read_text
read_bytes
read_json
write_text
write_bytes
write_json
mkdir
touch
exists
rename
unlink
rmdir
remove
stat
lstat
is_file
is_dir
is_symlink
is_fifo
is_mount
is_block_device
is_char_device
is_socket
Example
Some common using cases:
from pashlib import Path
from aiopathlib import AsyncPath
filename = 'test.json'
ap = AsyncPath(filename)
p = Path(filename)
assert (await ap.exists()) == p.exists() == False
await ap.touch() # Create a empty file
assert (await ap.is_file()) == p.is_file() == True
assert (await ap.is_dir()) == p.is_dir() == False
assert (await ap.is_symlink()) == p.is_symlink() == False
for func in ('is_fifo', 'is_mount', 'is_block_device', 'is_char_device', 'is_socket'):
assert (await getattr(ap, func)()) == getattr(p, func)()
d = {'key': 'value'}
await ap.write_json(d) # == p.write_text(json.dumps(d))
assert (await ap.read_json()) == d # == json.loads(p.read_text())
assert (await ap.read_bytes()) == p.read_bytes() # b'{"key": "value"}'
assert (await ap.stat()) == p.stat()
assert (await ap.lstat()) == p.lstat()
ap = await ap.rename('test_dir') # == AsyncPath(p.rename('test_dir'))
await ap.remove() # == await ap.unlink() == p.unlink()
await ap.mkdir() # == p.mkdir()
# Synchronization functions
[Path(i) for i in ap.glob('*')] == list(p.glob('*'))
[Path(i) for i in ap.rglob('*')] == list(p.rglob('*'))
ap / 'filename' == ap.joinpath('filename') == AsyncPath(f'{ap}/filename')
str(AsyncPath('string-or-Path-or-AsyncPath')) == str(Path('string-or-Path-or-AsyncPath'))
ap.stem == p.stem
ap.suffix == p.suffix
Path(ap.with_name('xxx')) == p.with_name('xxx')
Path(ap.parent) == p.parent
Path(ap.resolve()) == p.resolve()
...
History
0.3.1 (2022-02-20)
- Return content size after write local file
- Upgrade dependencies
0.3.0 (2021-12-16)
- Support Python3.7
- Clear
dev_requirements.txt
to be only package name and version
0.2.3 (2021-10-16)
- Make
touch
pass test for py39. - Remove support for pypy3 from docs.
0.2.2 (2021-09-20)
- Make
touch
/stat
/is_file
/... be awaitable. - Use
super().__new__
for initial.
0.2.0 (2021-08-29)
- Make
AsyncPath
be subclass ofpathlib.Path
. - Add github action to show test coverage.
0.1.3 (2021-08-28)
- Add makefile.
- Test all functions.
- Fix rename method error.
- Support sync pathlib methods.
0.1.0 (2021-06-14)
- Introduced a changelog.
- Publish at gitee.
Contributing
Contributions are very welcome.
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
aiopathlib-0.5.0.tar.gz
(7.0 kB
view details)
Built Distribution
File details
Details for the file aiopathlib-0.5.0.tar.gz
.
File metadata
- Download URL: aiopathlib-0.5.0.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b48e8dc02f18bf4a577c88a3eb04455d5deead11a22899514d765a84a3bc3805 |
|
MD5 | e89d14046e57440582dc98efa47dc5b2 |
|
BLAKE2b-256 | 2062bbe26a7960c8e3ac84769d32cf85e4bcfbd42d2fce9b2cedf013b5a1e989 |
File details
Details for the file aiopathlib-0.5.0-py3-none-any.whl
.
File metadata
- Download URL: aiopathlib-0.5.0-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f595fb7f159117961a5972e973ac3b54f8d47c7d677fd498856cf645c7030421 |
|
MD5 | 04c38e814bbfe05440f8aba62b8ee78a |
|
BLAKE2b-256 | 5f2f83e52619251be1573bc5770332a70451f964165cfd5073b00081b68e8ef4 |