Opening files¶
urio.open¶
Open path and return an object that is both awaitable and an async
context manager. Binary modes yield an AsyncBufferedFile; text
modes yield an AsyncTextFile.
Parameters¶
| Parameter | Description |
|---|---|
path |
Path to open. Accepts str, bytes, os.PathLike, or a urio.Path. |
mode |
Mode string (see below). Defaults to 'r' (text read). |
encoding |
Text mode only. Codec name; defaults to the locale preferred encoding. Ignored in binary mode. |
errors |
Text mode only. Codec error policy (e.g. 'strict', 'replace'). Defaults to 'strict'. |
newline |
Text mode only. Controls universal-newline behaviour (see text mode). |
Modes¶
| Char | Meaning |
|---|---|
r |
Open for reading (default). |
w |
Open for writing, truncating first. Creates if missing. |
a |
Open for writing, appending to the end. Creates if missing. |
x |
Exclusive creation; fails if the file exists. |
b |
Binary mode. |
t |
Text mode (default). |
+ |
Open for updating (reading and writing). |
Exactly one of r/w/a/x must be present. Text (t) is the default unless
b is given. Combining t and b, or supplying more than one primary mode,
raises ValueError.
Two ways to use it¶
As an async context manager (closes automatically):
As an awaitable (you close it yourself):
Return type by mode¶
| Mode contains | Returns |
|---|---|
b |
AsyncBufferedFile |
| otherwise (text) | AsyncTextFile |
Errors¶
ValueError— invalid or contradictory mode string.FileNotFoundError— opening a missing file for reading.FileExistsError—xmode when the file already exists.PermissionError,IsADirectoryError, etc. — surfaced from the OS as usual.