{"id":13434735,"url":"https://github.com/Tinche/aiofiles","last_synced_at":"2025-03-18T01:31:56.073Z","repository":{"id":29549012,"uuid":"33088125","full_name":"Tinche/aiofiles","owner":"Tinche","description":"File support for asyncio","archived":false,"fork":false,"pushed_at":"2025-02-08T11:31:28.000Z","size":212,"stargazers_count":2973,"open_issues_count":54,"forks_count":156,"subscribers_count":35,"default_branch":"main","last_synced_at":"2025-03-11T03:14:59.489Z","etag":null,"topics":["asyncio"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Tinche.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"tidelift":"pypi/aiofiles","github":"Tinche"}},"created_at":"2015-03-29T20:28:14.000Z","updated_at":"2025-03-11T00:57:23.000Z","dependencies_parsed_at":"2024-06-18T12:15:00.115Z","dependency_job_id":"666fe8a7-293c-4b0c-b1e0-209e8f0a4295","html_url":"https://github.com/Tinche/aiofiles","commit_stats":{"total_commits":241,"total_committers":41,"mean_commits":5.878048780487805,"dds":"0.44398340248962653","last_synced_commit":"307152616b7458e52e8c5e550fa0c7a926a3bc34"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tinche%2Faiofiles","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tinche%2Faiofiles/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tinche%2Faiofiles/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tinche%2Faiofiles/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Tinche","download_url":"https://codeload.github.com/Tinche/aiofiles/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244139302,"owners_count":20404491,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asyncio"],"created_at":"2024-07-31T03:00:21.715Z","updated_at":"2025-03-18T01:31:56.068Z","avatar_url":"https://github.com/Tinche.png","language":"Python","readme":"# aiofiles: file support for asyncio\n\n[![PyPI](https://img.shields.io/pypi/v/aiofiles.svg)](https://pypi.python.org/pypi/aiofiles)\n[![Build](https://github.com/Tinche/aiofiles/workflows/CI/badge.svg)](https://github.com/Tinche/aiofiles/actions)\n[![Coverage](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/Tinche/882f02e3df32136c847ba90d2688f06e/raw/covbadge.json)](https://github.com/Tinche/aiofiles/actions/workflows/main.yml)\n[![Supported Python versions](https://img.shields.io/pypi/pyversions/aiofiles.svg)](https://github.com/Tinche/aiofiles)\n[![Black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n\n**aiofiles** is an Apache2 licensed library, written in Python, for handling local\ndisk files in asyncio applications.\n\nOrdinary local file IO is blocking, and cannot easily and portably be made\nasynchronous. This means doing file IO may interfere with asyncio applications,\nwhich shouldn't block the executing thread. aiofiles helps with this by\nintroducing asynchronous versions of files that support delegating operations to\na separate thread pool.\n\n```python\nasync with aiofiles.open('filename', mode='r') as f:\n    contents = await f.read()\nprint(contents)\n'My file contents'\n```\n\nAsynchronous iteration is also supported.\n\n```python\nasync with aiofiles.open('filename') as f:\n    async for line in f:\n        ...\n```\n\nAsynchronous interface to tempfile module.\n\n```python\nasync with aiofiles.tempfile.TemporaryFile('wb') as f:\n    await f.write(b'Hello, World!')\n```\n\n## Features\n\n- a file API very similar to Python's standard, blocking API\n- support for buffered and unbuffered binary files, and buffered text files\n- support for `async`/`await` ([PEP 492](https://peps.python.org/pep-0492/)) constructs\n- async interface to tempfile module\n\n## Installation\n\nTo install aiofiles, simply:\n\n```bash\n$ pip install aiofiles\n```\n\n## Usage\n\nFiles are opened using the `aiofiles.open()` coroutine, which in addition to\nmirroring the builtin `open` accepts optional `loop` and `executor`\narguments. If `loop` is absent, the default loop will be used, as per the\nset asyncio policy. If `executor` is not specified, the default event loop\nexecutor will be used.\n\nIn case of success, an asynchronous file object is returned with an\nAPI identical to an ordinary file, except the following methods are coroutines\nand delegate to an executor:\n\n- `close`\n- `flush`\n- `isatty`\n- `read`\n- `readall`\n- `read1`\n- `readinto`\n- `readline`\n- `readlines`\n- `seek`\n- `seekable`\n- `tell`\n- `truncate`\n- `writable`\n- `write`\n- `writelines`\n\nIn case of failure, one of the usual exceptions will be raised.\n\n`aiofiles.stdin`, `aiofiles.stdout`, `aiofiles.stderr`,\n`aiofiles.stdin_bytes`, `aiofiles.stdout_bytes`, and\n`aiofiles.stderr_bytes` provide async access to `sys.stdin`,\n`sys.stdout`, `sys.stderr`, and their corresponding `.buffer` properties.\n\nThe `aiofiles.os` module contains executor-enabled coroutine versions of\nseveral useful `os` functions that deal with files:\n\n- `stat`\n- `statvfs`\n- `sendfile`\n- `rename`\n- `renames`\n- `replace`\n- `remove`\n- `unlink`\n- `mkdir`\n- `makedirs`\n- `rmdir`\n- `removedirs`\n- `link`\n- `symlink`\n- `readlink`\n- `listdir`\n- `scandir`\n- `access`\n- `getcwd`\n- `path.abspath`\n- `path.exists`\n- `path.isfile`\n- `path.isdir`\n- `path.islink`\n- `path.ismount`\n- `path.getsize`\n- `path.getatime`\n- `path.getctime`\n- `path.samefile`\n- `path.sameopenfile`\n\n### Tempfile\n\n**aiofiles.tempfile** implements the following interfaces:\n\n- TemporaryFile\n- NamedTemporaryFile\n- SpooledTemporaryFile\n- TemporaryDirectory\n\nResults return wrapped with a context manager allowing use with async with and async for.\n\n```python\nasync with aiofiles.tempfile.NamedTemporaryFile('wb+') as f:\n    await f.write(b'Line1\\n Line2')\n    await f.seek(0)\n    async for line in f:\n        print(line)\n\nasync with aiofiles.tempfile.TemporaryDirectory() as d:\n    filename = os.path.join(d, \"file.ext\")\n```\n\n### Writing tests for aiofiles\n\nReal file IO can be mocked by patching `aiofiles.threadpool.sync_open`\nas desired. The return type also needs to be registered with the\n`aiofiles.threadpool.wrap` dispatcher:\n\n```python\naiofiles.threadpool.wrap.register(mock.MagicMock)(\n    lambda *args, **kwargs: aiofiles.threadpool.AsyncBufferedIOBase(*args, **kwargs)\n)\n\nasync def test_stuff():\n    write_data = 'data'\n    read_file_chunks = [\n        b'file chunks 1',\n        b'file chunks 2',\n        b'file chunks 3',\n        b'',\n    ]\n    file_chunks_iter = iter(read_file_chunks)\n\n    mock_file_stream = mock.MagicMock(\n        read=lambda *args, **kwargs: next(file_chunks_iter)\n    )\n\n    with mock.patch('aiofiles.threadpool.sync_open', return_value=mock_file_stream) as mock_open:\n        async with aiofiles.open('filename', 'w') as f:\n            await f.write(write_data)\n            assert await f.read() == b'file chunks 1'\n\n        mock_file_stream.write.assert_called_once_with(write_data)\n```\n\n### Contributing\n\nContributions are very welcome. Tests can be run with `tox`, please ensure\nthe coverage at least stays the same before you submit a pull request.\n","funding_links":["https://tidelift.com/funding/github/pypi/aiofiles","https://github.com/sponsors/Tinche"],"categories":["HarmonyOS","Python","资源列表","Data Format \u0026 I/O","语言资源库","File \u0026 Path Utilities","File Manipulation"],"sub_categories":["Windows Manager","文件","For Python","python","HTTP Clients"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTinche%2Faiofiles","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FTinche%2Faiofiles","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTinche%2Faiofiles/lists"}