convert from reST to MyST
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by denuoweb · committed by denuoweb · parent f65da9b · Browse files · 1 file·+40 added−28 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+40 added−28 removed
- other:+40 added−28 removed
Provenance
- Commit
a31eb9b- Tree
793ede2- Author
denuoweb <jaron@rosenau.info>- Committer
denuoweb <jaron@rosenau.info>- Parent
f65da9b
1 file changed+40 added−28 removed
modified:docs/handling-files.md+40 added-28 removed
1123434656788910101112121313141515161718171819202122232425262713 unmodified lines414243444546444547484749504950515251525354555456575859606162636410 unmodified lines75767778798081827879838485868781888983909192939495968586979899888990100101102.. _handling-files:(handling-files)=
Handling Files================# Handling Files
.. currentmodule:: click```{currentmodule} click```
Click has built in features to support file and file path handling. The examples use arguments but the same principle applies to options as well.Click has built in features to support file and file path handling. The examples use arguments but the same principleapplies to options as well.
.. _file-args:(file-args)=
File Arguments-----------------## File Arguments
Click supports working with files with the :class:`File` type. Some notable features are:Click supports working with files with the {class}`File` type. Some notable features are:
* Support for ``-`` to mean a special file that refers to stdin when used for reading, and stdout when used for writing. This is a common pattern for POSIX command line utilities.* Deals with ``str`` and ``bytes`` correctly for all versions of Python.- Support for `-` to mean a special file that refers to stdin when used for reading, and stdout when used for writing. This is a common pattern for POSIX command line utilities.- Deals with `str` and `bytes` correctly for all versions of Python.
Example:
```{eval-rst}.. click:example::
@click.command() @click.argument('input', type=click.File('rb'))13 unmodified lines with isolated_filesystem(): invoke(inout, args=['-', 'hello.txt'], input=['hello'], terminate_input=True) invoke(inout, args=['hello.txt', '-'])```
File Path Arguments----------------------## File Path Arguments
For handling paths, the :class:`Path` type is better than a ``str``. Some notable features are:For handling paths, the {class}`Path` type is better than a `str`. Some notable features are:
* The ``exists`` argument will verify whether the path exists.* ``readable``, ``writable``, and ``executable`` can perform permission checks.* ``file_okay`` and ``dir_okay`` allow specifying whether files/directories are accepted.* Error messages are nicely formatted using :func:`format_filename` so any undecodable bytes will be printed nicely.- The `exists` argument will verify whether the path exists.- `readable`, `writable`, and `executable` can perform permission checks.- `file_okay` and `dir_okay` allow specifying whether files/directories are accepted.- Error messages are nicely formatted using {func}`format_filename` so any undecodable bytes will be printed nicely.
See :class:`Path` for all features.See {class}`Path` for all features.
Example:
```{eval-rst}.. click:example::
@click.command() @click.argument('filename', type=click.Path(exists=True))10 unmodified lines f.write('Hello World!\n') invoke(touch, args=['hello.txt']) println() invoke(touch, args=['missing.txt'])```
## File Opening Behaviors
File Opening Behaviors-----------------------------The {class}`File` type attempts to be "intelligent" about when to open a file. Stdin/stdout and files opened for readingwill be opened immediately. This will give the user direct feedback when a file cannot be opened. Files opened forwriting will only be open on the first IO operation. This is done by automatically wrapping the file in a specialwrapper.
The :class:`File` type attempts to be "intelligent" about when to open a file. Stdin/stdout and files opened for reading will be opened immediately. This will give the user direct feedback when a file cannot be opened. Files opened for writing will only be open on the first IO operation. This is done by automatically wrapping the file in a special wrapper.File open behavior can be controlled by the boolean kwarg `lazy`. If a file is opened lazily:
File open behavior can be controlled by the boolean kwarg ``lazy``. If a file is opened lazily:- A failure at first IO operation will happen by raising an {exc}`FileError`.- It can help minimize resource handling confusion. If a file is opened in lazy mode, it will call {meth}`LazyFile.close_intelligently` to help figure out if the file needs closing or not. This is not needed for parameters, but is necessary for manually prompting. For manual prompts with the {func}`prompt` function you do not know if a stream like stdout was opened (which was already open before) or a real file was opened (that needs closing).
* A failure at first IO operation will happen by raising an :exc:`FileError`.* It can help minimize resource handling confusion. If a file is opened in lazy mode, it will call :meth:`LazyFile.close_intelligently` to help figure out if the file needs closing or not. This is not needed for parameters, but is necessary for manually prompting. For manual prompts with the :func:`prompt` function you do not know if a stream like stdout was opened (which was already open before) or a real file was opened (that needs closing).Since files opened for writing will typically empty the file, the lazy mode should only be disabled if the developer isabsolutely sure that this is intended behavior.
Since files opened for writing will typically empty the file, the lazy mode should only be disabled if the developer is absolutely sure that this is intended behavior.
It is also possible to open files in atomic mode by passing ``atomic=True``. In atomic mode, all writes go into a separate file in the same folder, and upon completion, the file will be moved over to the original location. This is useful if a file regularly read by other users is modified.It is also possible to open files in atomic mode by passing `atomic=True`. In atomic mode, all writes go into a separatefile in the same folder, and upon completion, the file will be moved over to the original location. This is useful if afile regularly read by other users is modified.