class File(ParamType):
Declares a parameter to be a file for reading or writing. The file is automatically closed once the context tears down (after the command finished working).
Files can be opened for reading or writing. The special value - indicates stdin or stdout depending on the mode.
By default, the file is opened for reading text data, but it can also be opened in binary mode or for writing. The encoding parameter can be used to force a specific encoding.
The lazy
flag controls if the file should be opened immediately or upon
first IO. The default is to be non-lazy for standard input and output
streams as well as files opened for reading, lazy
otherwise. When opening a
file lazily for reading, it is still opened temporarily for validation, but
will not be held open until first IO. lazy is mainly useful when opening
for writing to avoid creating the file until it is needed.
Starting with Click 2.0, files can also be opened atomically in which case 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.
See :ref:`file-args` for more information.
Method | __init__ |
Undocumented |
Method | convert |
Convert the value to the correct type. This is not called if the value is None (the missing value). |
Method | resolve_lazy_flag |
Undocumented |
Method | shell_complete |
Return a special completion marker that tells the completion system to use the shell to provide file path completions. |
Method | to_info_dict |
Gather information that could be useful for a tool generating user-facing documentation. |
Class Variable | name |
Undocumented |
Instance Variable | atomic |
Undocumented |
Instance Variable | encoding |
Undocumented |
Instance Variable | errors |
Undocumented |
Instance Variable | lazy |
Undocumented |
Instance Variable | mode |
Undocumented |
Inherited from ParamType
:
Method | __call__ |
Undocumented |
Method | fail |
Helper method to fail with an invalid value message. |
Method | get_metavar |
Returns the metavar default for this param if it provides one. |
Method | get_missing_message |
Optionally might return extra information about a missing parameter. |
Method | split_envvar_value |
Given a value from an environment variable this splits it up into small chunks depending on the defined envvar list splitter. |
Class Variable | arity |
Undocumented |
Class Variable | envvar_list_splitter |
Undocumented |
Class Variable | is_composite |
Undocumented |
Undocumented
Parameters | |
mode:str | Undocumented |
encoding:t.Optional[ | Undocumented |
errors:t.Optional[ | Undocumented |
lazy:t.Optional[ | Undocumented |
atomic:bool | Undocumented |
click.types.ParamType.convert
Convert the value to the correct type. This is not called if the value is None (the missing value).
This must accept string values from the command line, as well as values that are already the correct type. It may also convert other compatible types.
The param and ctx arguments may be None in certain situations, such as when converting prompt input.
If the value cannot be converted, call fail
with a
descriptive message.
Parameters | |
value:t.Any | The value to convert. |
param:t.Optional[ | The parameter that is using this type to convert its value. May be None. |
ctx:t.Optional[ | The current context that arrived at this value. May be None. |
Returns | |
t.Any | Undocumented |
Undocumented
Parameters | |
value:t.Any | Undocumented |
Returns | |
bool | Undocumented |
click.types.ParamType.shell_complete
Return a special completion marker that tells the completion system to use the shell to provide file path completions.
Parameters | |
ctx:Context | Invocation context for this command. |
param:Parameter | The parameter that is requesting completion. |
incomplete:str | Value being completed. May be empty. |
Returns | |
t.List[ | Undocumented |
click.types.ParamType.to_info_dict
Gather information that could be useful for a tool generating user-facing documentation.
Use click.Context.to_info_dict
to traverse the entire
CLI structure.
Returns | |
t.Dict[ | Undocumented |