aiopathlib


Nameaiopathlib JSON
Version 0.6.0 PyPI version JSON
download
home_pageNone
SummaryPathlib support for asyncio
upload_time2025-08-01 12:08:14
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseMIT
keywords aiopathlib asyncpath aiopath
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            aiopathlib: Pathlib support for asyncio
=======================================

[![image](https://img.shields.io/pypi/v/aiopathlib.svg)](https://pypi.org/project/aiopathlib/)
[![image](https://img.shields.io/pypi/pyversions/aiopathlib.svg)](https://pypi.org/project/aiopathlib/)
[![image](https://img.shields.io/pypi/l/aiopathlib.svg)](https://pypi.org/project/aiopathlib/)
[![image](https://img.shields.io/codecov/c/github/waketzheng/aiopathlib/master.svg)](https://codecov.io/github/waketzheng/aiopathlib?branch=master)
[![image](https://img.shields.io/badge/code%20style-pep8-green.svg)](https://www.python.org/dev/peps/pep-0008/)

> If you are not using aiofiles, `anyio.Path` is another choice:

Install:
```bash
pip install anyio
```
Examples:
```py
from datetime import datetime
from anyio import Path

filepath = Path(__file__)
another = filepath.parent / 'sub_dirpath' / 'filename.ext'

if await another.exists():
    content = await another.read_bytes()
elif not await another.parent.exists():
    await another.parent.mkdir(parents=True)
else:
    await another.write_bytes(b'1')

# glob/stat/remove
async for p in filepath.parent.glob('*'):
    if await p.is_file():
        stat = await p.stat()
        create_time = datetime.fromtimestamp(stat.st_ctime)
        update_time = datetime.fromtimestamp(stat.st_mtime)
        await p.unlink()  # remove file
        print(f'{p} created at: {create_time}, modified at: {update_time}, removed at: {datetime.now()}')
```
See more at: https://github.com/agronholm/anyio

------
**aiopathlib** is written in Python, for handling local
disk files in asyncio applications.

Base on [aiofiles](https://github.com/Tinche/aiofiles) and just like pathlib, but use await.

```py
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.

```py
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:


```bash
$ 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 of `pathlib.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.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "aiopathlib",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "aiopathlib, asyncpath, aiopath",
    "author": null,
    "author_email": "Waket Zheng <waketzheng@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/9b/77/4de0b200a8e841dbf3617cac1fd4b7177e5c7914a4a43bd9397e25bd21c8/aiopathlib-0.6.0.tar.gz",
    "platform": null,
    "description": "aiopathlib: Pathlib support for asyncio\n=======================================\n\n[![image](https://img.shields.io/pypi/v/aiopathlib.svg)](https://pypi.org/project/aiopathlib/)\n[![image](https://img.shields.io/pypi/pyversions/aiopathlib.svg)](https://pypi.org/project/aiopathlib/)\n[![image](https://img.shields.io/pypi/l/aiopathlib.svg)](https://pypi.org/project/aiopathlib/)\n[![image](https://img.shields.io/codecov/c/github/waketzheng/aiopathlib/master.svg)](https://codecov.io/github/waketzheng/aiopathlib?branch=master)\n[![image](https://img.shields.io/badge/code%20style-pep8-green.svg)](https://www.python.org/dev/peps/pep-0008/)\n\n> If you are not using aiofiles, `anyio.Path` is another choice:\n\nInstall:\n```bash\npip install anyio\n```\nExamples:\n```py\nfrom datetime import datetime\nfrom anyio import Path\n\nfilepath = Path(__file__)\nanother = filepath.parent / 'sub_dirpath' / 'filename.ext'\n\nif await another.exists():\n    content = await another.read_bytes()\nelif not await another.parent.exists():\n    await another.parent.mkdir(parents=True)\nelse:\n    await another.write_bytes(b'1')\n\n# glob/stat/remove\nasync for p in filepath.parent.glob('*'):\n    if await p.is_file():\n        stat = await p.stat()\n        create_time = datetime.fromtimestamp(stat.st_ctime)\n        update_time = datetime.fromtimestamp(stat.st_mtime)\n        await p.unlink()  # remove file\n        print(f'{p} created at: {create_time}, modified at: {update_time}, removed at: {datetime.now()}')\n```\nSee more at: https://github.com/agronholm/anyio\n\n------\n**aiopathlib** is written in Python, for handling local\ndisk files in asyncio applications.\n\nBase on [aiofiles](https://github.com/Tinche/aiofiles) and just like pathlib, but use await.\n\n```py\nwith open('filename', 'w') as fp:\n    fp.write('My file contents')\n\ntext = await aiopathlib.AsyncPath('filename').read_text()\nprint(text)\n'My file contents'\n\ncontent = await aiopathlib.AsyncPath(Path('filename')).read_bytes()\nprint(content)\nb'My file contents'\n```\n\nAsynchronous interface to create folder.\n\n```py\nfrom aiopathlib import AsyncPath\n\napath = AsyncPath('dirname/subpath')\nif not await apath.exists():\n    await apath.mkdir(parents=True)\n```\n\n\nFeatures\n--------\n\n- a file API very similar to Python's standard package `pathlib`, blocking API\n- support for buffered and unbuffered binary files, and buffered text files\n- support for ``async``/``await`` (:PEP:`492`) constructs\n\n\nInstallation\n------------\n\nTo install aiopathlib, simply:\n\n\n```bash\n$ pip install aiopathlib\n```\n\n\nUsage\n-----\nThese functions are awaitable\n\n* ``read_text``\n* ``read_bytes``\n* ``read_json``\n* ``write_text``\n* ``write_bytes``\n* ``write_json``\n* ``mkdir``\n* ``touch``\n* ``exists``\n* ``rename``\n* ``unlink``\n* ``rmdir``\n* ``remove``\n* ``stat``\n* ``lstat``\n* ``is_file``\n* ``is_dir``\n* ``is_symlink``\n* ``is_fifo``\n* ``is_mount``\n* ``is_block_device``\n* ``is_char_device``\n* ``is_socket``\n\nExample\n-------\nSome common using cases:\n\n```\nfrom pashlib import Path\nfrom aiopathlib import AsyncPath\n\nfilename = 'test.json'\nap = AsyncPath(filename)\np = Path(filename)\nassert (await ap.exists()) == p.exists() == False\nawait ap.touch()  # Create a empty file\nassert (await ap.is_file()) == p.is_file() == True\nassert (await ap.is_dir()) == p.is_dir() == False\nassert (await ap.is_symlink()) == p.is_symlink() == False\nfor func in ('is_fifo', 'is_mount', 'is_block_device', 'is_char_device', 'is_socket'):\n    assert (await getattr(ap, func)()) == getattr(p, func)()\nd = {'key': 'value'}\nawait ap.write_json(d)  # == p.write_text(json.dumps(d))\nassert (await ap.read_json()) == d  # == json.loads(p.read_text())\nassert (await ap.read_bytes()) == p.read_bytes()  # b'{\"key\": \"value\"}'\nassert (await ap.stat()) == p.stat()\nassert (await ap.lstat()) == p.lstat()\nap = await ap.rename('test_dir')  # == AsyncPath(p.rename('test_dir'))\nawait ap.remove()  # == await ap.unlink() == p.unlink()\nawait ap.mkdir()  # == p.mkdir()\n\n# Synchronization functions\n[Path(i) for i in ap.glob('*')] == list(p.glob('*'))\n[Path(i) for i in ap.rglob('*')] == list(p.rglob('*'))\nap / 'filename' == ap.joinpath('filename') == AsyncPath(f'{ap}/filename')\nstr(AsyncPath('string-or-Path-or-AsyncPath')) == str(Path('string-or-Path-or-AsyncPath'))\nap.stem == p.stem\nap.suffix == p.suffix\nPath(ap.with_name('xxx')) == p.with_name('xxx')\nPath(ap.parent) == p.parent\nPath(ap.resolve()) == p.resolve()\n...\n```\n\n\nHistory\n-------\n\n#### 0.3.1 (2022-02-20)\n\n- Return content size after write local file\n- Upgrade dependencies\n\n#### 0.3.0 (2021-12-16)\n\n- Support Python3.7\n- Clear `dev_requirements.txt` to be only package name and version\n\n#### 0.2.3 (2021-10-16)\n\n- Make `touch` pass test for py39.\n- Remove support for pypy3 from docs.\n\n#### 0.2.2 (2021-09-20)\n\n- Make `touch`/`stat`/`is_file`/... be awaitable.\n- Use `super().__new__` for initial.\n\n#### 0.2.0 (2021-08-29)\n\n- Make `AsyncPath` be subclass of `pathlib.Path`.\n- Add github action to show test coverage.\n\n#### 0.1.3 (2021-08-28)\n\n- Add makefile.\n- Test all functions.\n- Fix rename method error.\n- Support sync pathlib methods.\n\n#### 0.1.0 (2021-06-14)\n\n- Introduced a changelog.\n- Publish at gitee.\n\n\nContributing\n------------\n\nContributions are very welcome.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Pathlib support for asyncio",
    "version": "0.6.0",
    "project_urls": {
        "Bug Tracker": "https://github.com/waketzheng/aiopathlib/issues",
        "homepage": "https://github.com/waketzheng/aiopathlib",
        "repository": "https://github.com/waketzheng/aiopathlib.git"
    },
    "split_keywords": [
        "aiopathlib",
        " asyncpath",
        " aiopath"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0928d212c070c5d1b397c6b341a69e6694743e1f833f6f3a5f106f22842aa753",
                "md5": "4a1b15035c650ed88a0b1bedffef0f5b",
                "sha256": "56fd3b4a22f161569f04437bb9c2ab868a84090bf5adc7b1a2401e1e8a098e5f"
            },
            "downloads": -1,
            "filename": "aiopathlib-0.6.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "4a1b15035c650ed88a0b1bedffef0f5b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 6509,
            "upload_time": "2025-08-01T12:08:13",
            "upload_time_iso_8601": "2025-08-01T12:08:13.653520Z",
            "url": "https://files.pythonhosted.org/packages/09/28/d212c070c5d1b397c6b341a69e6694743e1f833f6f3a5f106f22842aa753/aiopathlib-0.6.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9b774de0b200a8e841dbf3617cac1fd4b7177e5c7914a4a43bd9397e25bd21c8",
                "md5": "19f6e21a3be091e5b5ca53d0c2455b17",
                "sha256": "b843e5efc31df04929c8bd644adc2c347a06f72c1083a90597649c3566dd3eea"
            },
            "downloads": -1,
            "filename": "aiopathlib-0.6.0.tar.gz",
            "has_sig": false,
            "md5_digest": "19f6e21a3be091e5b5ca53d0c2455b17",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 9602,
            "upload_time": "2025-08-01T12:08:14",
            "upload_time_iso_8601": "2025-08-01T12:08:14.655272Z",
            "url": "https://files.pythonhosted.org/packages/9b/77/4de0b200a8e841dbf3617cac1fd4b7177e5c7914a4a43bd9397e25bd21c8/aiopathlib-0.6.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-01 12:08:14",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "waketzheng",
    "github_project": "aiopathlib",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "aiopathlib"
}
        
Elapsed time: 1.61223s