switch extension from .rst to .md
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by denuoweb · committed by denuoweb · parent 0df309d · Browse files · 2 files·+90 added−90 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+90 added−90 removed
- other:+90 added−90 removed
Provenance
- Commit
f65da9b- Tree
e53f8fa- Author
denuoweb <jaron@rosenau.info>- Committer
denuoweb <jaron@rosenau.info>- Parent
0df309d
2 files changed+90 added−90 removed
added:docs/handling-files.md+90 added
1+.. _handling-files:2+3+Handling Files4+================5+6+.. currentmodule:: click7+8+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.9+10+.. _file-args:11+12+File Arguments13+-----------------14+15+Click supports working with files with the :class:`File` type. Some notable features are:16+17+* 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.18+* Deals with ``str`` and ``bytes`` correctly for all versions of Python.19+20+Example:21+22+.. click:example::23+24+ @click.command()25+ @click.argument('input', type=click.File('rb'))26+ @click.argument('output', type=click.File('wb'))27+ def inout(input, output):28+ """Copy contents of INPUT to OUTPUT."""29+ while True:30+ chunk = input.read(1024)31+ if not chunk:32+ break33+ output.write(chunk)34+35+And from the command line:36+37+.. click:run::38+39+ with isolated_filesystem():40+ invoke(inout, args=['-', 'hello.txt'], input=['hello'],41+ terminate_input=True)42+ invoke(inout, args=['hello.txt', '-'])43+44+File Path Arguments45+----------------------46+47+For handling paths, the :class:`Path` type is better than a ``str``. Some notable features are:48+49+* The ``exists`` argument will verify whether the path exists.50+* ``readable``, ``writable``, and ``executable`` can perform permission checks.51+* ``file_okay`` and ``dir_okay`` allow specifying whether files/directories are accepted.52+* Error messages are nicely formatted using :func:`format_filename` so any undecodable bytes will be printed nicely.53+54+See :class:`Path` for all features.55+56+Example:57+58+.. click:example::59+60+ @click.command()61+ @click.argument('filename', type=click.Path(exists=True))62+ def touch(filename):63+ """Print FILENAME if the file exists."""64+ click.echo(click.format_filename(filename))65+66+And from the command line:67+68+.. click:run::69+70+ with isolated_filesystem():71+ with open('hello.txt', 'w') as f:72+ f.write('Hello World!\n')73+ invoke(touch, args=['hello.txt'])74+ println()75+ invoke(touch, args=['missing.txt'])76+77+78+File Opening Behaviors79+-----------------------------80+81+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.82+83+File open behavior can be controlled by the boolean kwarg ``lazy``. If a file is opened lazily:84+85+* A failure at first IO operation will happen by raising an :exc:`FileError`.86+* 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).87+88+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.89+90+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.deleted:docs/handling-files.rst-90 removed
1-.. _handling-files:2-3-Handling Files4-================5-6-.. currentmodule:: click7-8-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.9-10-.. _file-args:11-12-File Arguments13------------------14-15-Click supports working with files with the :class:`File` type. Some notable features are:16-17-* 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.18-* Deals with ``str`` and ``bytes`` correctly for all versions of Python.19-20-Example:21-22-.. click:example::23-24- @click.command()25- @click.argument('input', type=click.File('rb'))26- @click.argument('output', type=click.File('wb'))27- def inout(input, output):28- """Copy contents of INPUT to OUTPUT."""29- while True:30- chunk = input.read(1024)31- if not chunk:32- break33- output.write(chunk)34-35-And from the command line:36-37-.. click:run::38-39- with isolated_filesystem():40- invoke(inout, args=['-', 'hello.txt'], input=['hello'],41- terminate_input=True)42- invoke(inout, args=['hello.txt', '-'])43-44-File Path Arguments45-----------------------46-47-For handling paths, the :class:`Path` type is better than a ``str``. Some notable features are:48-49-* The ``exists`` argument will verify whether the path exists.50-* ``readable``, ``writable``, and ``executable`` can perform permission checks.51-* ``file_okay`` and ``dir_okay`` allow specifying whether files/directories are accepted.52-* Error messages are nicely formatted using :func:`format_filename` so any undecodable bytes will be printed nicely.53-54-See :class:`Path` for all features.55-56-Example:57-58-.. click:example::59-60- @click.command()61- @click.argument('filename', type=click.Path(exists=True))62- def touch(filename):63- """Print FILENAME if the file exists."""64- click.echo(click.format_filename(filename))65-66-And from the command line:67-68-.. click:run::69-70- with isolated_filesystem():71- with open('hello.txt', 'w') as f:72- f.write('Hello World!\n')73- invoke(touch, args=['hello.txt'])74- println()75- invoke(touch, args=['missing.txt'])76-77-78-File Opening Behaviors79------------------------------80-81-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.82-83-File open behavior can be controlled by the boolean kwarg ``lazy``. If a file is opened lazily:84-85-* A failure at first IO operation will happen by raising an :exc:`FileError`.86-* 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).87-88-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.89-90-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.