handling-files.md
This file
Path
docs/handling-files.md
101 lines · 3.5 KiB · Markdown
Last changed by
Mark clearly functions private status. Deprecated Python 2 utilities. (#3695)Rowlando13before adoption333c28d
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102(handling-files)=
# Handling Files
```{currentmodule} click```
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 Arguments
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.
Example:
```{eval-rst}.. click:example::
@click.command() @click.argument('input', type=click.File('rb')) @click.argument('output', type=click.File('wb')) def inout(input, output): """Copy contents of INPUT to OUTPUT.""" while True: chunk = input.read(1024) if not chunk: break output.write(chunk)
And from the command line:
.. click:run::
with isolated_filesystem(): invoke(inout, args=['-', 'hello.txt'], input=['hello'], terminate_input=True) invoke(inout, args=['hello.txt', '-'])```
## File Path Arguments
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.
See {class}`Path` for all features.
Example:
```{eval-rst}.. click:example::
@click.command() @click.argument('filename', type=click.Path(exists=True)) def touch(filename): """Print FILENAME if the file exists.""" click.echo(click.format_filename(filename))
And from the command line:
.. click:run::
with isolated_filesystem(): with open('hello.txt', 'w') as f: f.write('Hello World!\n') invoke(touch, args=['hello.txt']) println() invoke(touch, args=['missing.txt'])```
## 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.
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 by lazily closing it. 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.
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.