Open a file, with extra behavior to handle ``'-'`` to indicate a standard stream, lazy open on write, and atomic write. Similar to the behavior of the :class:`~click.File` param type. If ``'-'`` is given to open ``stdout`` or ``stdin``, the stream is wrapped so that using it in a co
(
filename: str | os.PathLike[str],
mode: str = "r",
encoding: str | None = None,
errors: str | None = "strict",
lazy: bool = False,
atomic: bool = False,
)
| 380 | |
| 381 | |
| 382 | def open_file( |
| 383 | filename: str | os.PathLike[str], |
| 384 | mode: str = "r", |
| 385 | encoding: str | None = None, |
| 386 | errors: str | None = "strict", |
| 387 | lazy: bool = False, |
| 388 | atomic: bool = False, |
| 389 | ) -> t.IO[t.Any]: |
| 390 | """Open a file, with extra behavior to handle ``'-'`` to indicate |
| 391 | a standard stream, lazy open on write, and atomic write. Similar to |
| 392 | the behavior of the :class:`~click.File` param type. |
| 393 | |
| 394 | If ``'-'`` is given to open ``stdout`` or ``stdin``, the stream is |
| 395 | wrapped so that using it in a context manager will not close it. |
| 396 | This makes it possible to use the function without accidentally |
| 397 | closing a standard stream: |
| 398 | |
| 399 | .. code-block:: python |
| 400 | |
| 401 | with open_file(filename) as f: |
| 402 | ... |
| 403 | |
| 404 | :param filename: The name or Path of the file to open, or ``'-'`` for |
| 405 | ``stdin``/``stdout``. |
| 406 | :param mode: The mode in which to open the file. |
| 407 | :param encoding: The encoding to decode or encode a file opened in |
| 408 | text mode. |
| 409 | :param errors: The error handling mode. |
| 410 | :param lazy: Wait to open the file until it is accessed. For read |
| 411 | mode, the file is temporarily opened to raise access errors |
| 412 | early, then closed until it is read again. |
| 413 | :param atomic: Write to a temporary file and replace the given file |
| 414 | on close. |
| 415 | |
| 416 | .. versionadded:: 3.0 |
| 417 | """ |
| 418 | if lazy: |
| 419 | return t.cast( |
| 420 | "t.IO[t.Any]", LazyFile(filename, mode, encoding, errors, atomic=atomic) |
| 421 | ) |
| 422 | |
| 423 | f, should_close = open_stream(filename, mode, encoding, errors, atomic=atomic) |
| 424 | |
| 425 | if not should_close: |
| 426 | f = t.cast("t.IO[t.Any]", KeepOpenFile(f)) |
| 427 | |
| 428 | return f |
| 429 | |
| 430 | |
| 431 | def format_filename( |
nothing calls this directly
no test coverage detected