class documentation

class File(ParamType):

View In Hierarchy

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
def __init__(self, mode='r', encoding=None, errors='strict', lazy=None, atomic=False):

Undocumented

Parameters
mode:strUndocumented
encoding:t.Optional[str]Undocumented
errors:t.Optional[str]Undocumented
lazy:t.Optional[bool]Undocumented
atomic:boolUndocumented
def convert(self, value, param, ctx):

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.AnyThe value to convert.
param:t.Optional[Parameter]The parameter that is using this type to convert its value. May be None.
ctx:t.Optional[Context]The current context that arrived at this value. May be None.
Returns
t.AnyUndocumented
def resolve_lazy_flag(self, value):

Undocumented

Parameters
value:t.AnyUndocumented
Returns
boolUndocumented
def shell_complete(self, ctx, param, incomplete):

Return a special completion marker that tells the completion system to use the shell to provide file path completions.

New in version 8.0.
Parameters
ctx:ContextInvocation context for this command.
param:ParameterThe parameter that is requesting completion.
incomplete:strValue being completed. May be empty.
Returns
t.List[CompletionItem]Undocumented
def to_info_dict(self):

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.

New in version 8.0.
Returns
t.Dict[str, t.Any]Undocumented
name: str =

Undocumented

atomic =

Undocumented

encoding =

Undocumented

errors =

Undocumented

lazy =

Undocumented

mode =

Undocumented