filepath

A text prompt which provides auto completion for system paths.

Example

demo

Classic Syntax (PyInquirer)
import os

from InquirerPy import prompt
from InquirerPy.validator import PathValidator


def main():
    home_path = "~/" if os.name == "posix" else "C:\\"
    questions = [
        {
            "type": "filepath",
            "message": "Enter file to upload:",
            "name": "location",
            "default": home_path,
            "validate": PathValidator(is_file=True, message="Input is not a file"),
            "only_files": True,
        },
        {
            "type": "filepath",
            "message": "Enter path to download:",
            "validate": PathValidator(is_dir=True, message="Input is not a directory"),
            "name": "destination",
            "only_directories": True,
        },
    ]

    result = prompt(questions)


if __name__ == "__main__":
    main()
Alternate Syntax
import os

from InquirerPy import inquirer
from InquirerPy.validator import PathValidator


def main():
    home_path = "~/" if os.name == "posix" else "C:\\"
    src_path = inquirer.filepath(
        message="Enter file to upload:",
        default=home_path,
        validate=PathValidator(is_file=True, message="Input is not a file"),
        only_files=True,
    ).execute()
    dest_path = inquirer.filepath(
        message="Enter path to download:",
        validate=PathValidator(is_dir=True, message="Input is not a directory"),
        only_directories=True,
    ).execute()


if __name__ == "__main__":
    main()

Keybindings

See also

keybindings

{
    "answer": [{"key": "enter"}],   # answer the prompt
    "interrupt": [{"key": "c-c"}],  # raise KeyboardInterrupt
    "skip": [{"key": "c-z"}],   # skip the prompt
}

In addition the default keybindings, you can use ctrl-space to trigger completion window popup.

{
    "completion": [{"key": "c-space"}]  # force completion popup
}

Symbols and ENV Variables

The auto completion can handle ~ and will start triggering completion for the home directory. However it does not handle ENV variable such as $HOME.

If you wish to support ENV variables completion, look into prompt_toolkit documentation and create a custom completion class. Directly use the pages/prompts/input:InputPrompt with the parameter completer.

Excluding File Types

This class contains 2 basic variables only_directories and only_files that can control whether to only list files or directories in the completion.

Note

only_directories takes higher priority over only_files.

See also

Example

Reference

class InquirerPy.prompts.filepath.FilePathPrompt(message, style=None, vi_mode=False, default='', qmark='?', amark='?', instruction='', long_instruction='', multicolumn_complete=False, validate=None, invalid_message='Invalid input', only_directories=False, only_files=False, transformer=None, filter=None, keybindings=None, wrap_lines=True, raise_keyboard_interrupt=True, mandatory=True, mandatory_message='Mandatory prompt', session_result=None, input=None, output=None)[source]

Create a prompt that provides auto completion for system filepaths.

A wrapper class around PromptSession.

Parameters
  • message (Union[str, Callable[[InquirerPySessionResult], str]]) – The question to ask the user. Refer to message documentation for more details.

  • style (Optional[InquirerPy.utils.InquirerPyStyle]) – An InquirerPyStyle instance. Refer to Style documentation for more details.

  • vi_mode (bool) – Use vim keybinding for the prompt. Refer to keybindings documentation for more details.

  • default (Union[Any, Callable[[InquirerPySessionResult], Any]]) – Set the default text value of the prompt. Refer to default documentation for more details.

  • qmark (str) – Question mark symbol. Custom symbol that will be displayed infront of the question before its answered.

  • amark (str) – Answer mark symbol. Custom symbol that will be displayed infront of the question after its answered.

  • instruction (str) – Short instruction to display next to the question.

  • long_instruction (str) – Long instructions to display at the bottom of the prompt.

  • multicolumn_complete (bool) – Change the auto-completion UI to a multi column display.

  • validate (Optional[Union[Callable[[Any], bool], Validator]]) – Add validation to user input. Refer to Validator documentation for more details.

  • invalid_message (str) – Error message to display when user input is invalid. Refer to Validator documentation for more details.

  • transformer (Optional[Callable[[str], Any]]) – A function which performs additional transformation on the value that gets printed to the terminal. Different than filter parameter, this is only visual effect and won’t affect the actual value returned by execute(). Refer to transformer documentation for more details.

  • filter (Optional[Callable[[str], Any]]) – A function which performs additional transformation on the result. This affects the actual value returned by execute(). Refer to filter documentation for more details.

  • keybindings (Optional[Dict[str, List[Dict[str, Union[str, FilterOrBool, List[str]]]]]]) – Customise the builtin keybindings. Refer to keybindings for more details.

  • wrap_lines (bool) – Soft wrap question lines when question exceeds the terminal width.

  • only_directories (bool) – Only complete directories.

  • only_files (bool) – Only complete files.

  • raise_keyboard_interrupt (bool) – Raise the KeyboardInterrupt exception when ctrl-c is pressed. If false, the result will be None and the question is skiped.

  • mandatory (bool) – Indicate if the prompt is mandatory. If True, then the question cannot be skipped.

  • mandatory_message (str) – Error message to show when user attempts to skip mandatory prompt.

  • session_result (Optional[Dict[Union[str, int], Optional[Union[str, bool, List[Any]]]]]) – Used internally for Classic Syntax (PyInquirer).

  • input (Optional[Input]) – Used internally and will be removed in future updates.

  • output (Optional[Output]) – Used internally and will be removed in future updates.

Return type

None

Examples

>>> from InquirerPy import inquirer
>>> result = inquirer.filepath(message="Enter a path:").execute()
>>> print(result)
/home/ubuntu/README.md