# Buggy code
async def _write_nlu_to_file(
    export_nlu_path: Text, events: List[Dict[Text, Any]]
) -> None:
    """Write the nlu data of the sender_id to the file paths."""
    from rasa.nlu.training_data import TrainingData

    msgs = _collect_messages(events)

    # noinspection PyBroadException
    try:
        previous_examples = loading.load_data(export_nlu_path)
    except Exception as e:
        logger.exception("An exception occurred while trying to load the NLU data.")

        export_nlu_path = questionary.text(
            message="Could not load existing NLU data, please "
            "specify where to store NLU data learned in "
            "this session (this will overwrite any "
            "existing file). {}".format(str(e)),
            default=PATHS["backup"],
        ).ask()

        if export_nlu_path is None:
            return

        previous_examples = TrainingData()

    nlu_data = previous_examples.merge(TrainingData(msgs))

    # need to guess the format of the file before opening it to avoid a read
    # in a write
    if loading.guess_format(export_nlu_path) in {"md", "unk"}:
        fformat = "md"
    else:
        fformat = "json"

    with open(export_nlu_path, "w", encoding="utf-8") as f:
        if fformat == "md":
            f.write(nlu_data.as_markdown())
        else:
            f.write(nlu_data.as_json())


# Traceback (FileNotFoundError)
? Export stories to (if file exists, this will append the stories)
? Export NLU data to (if file exists, this will merge learned data with previous training examples)
? Export domain file to (if file exists, this will be overwritten) domain.yml
2019-06-18 08:16:35 ERROR    rasa.core.training.interactive  - An exception occurred while recording messages.
Traceback (most recent call last):
File "/Users/erohmensing/rasa/rasa/core/training/interactive.py", line 1367, in record_messages
endpoint, sender_id, sender_ids, plot_file
File "/Users/erohmensing/rasa/rasa/core/training/interactive.py", line 885, in _predict_till_next_listen
action_name, policy, confidence, predictions, endpoint, sender_id
File "/Users/erohmensing/rasa/rasa/core/training/interactive.py", line 1024, in _validate_action
is_correct = await _ask_questions(question, sender_id, endpoint)
File "/Users/erohmensing/rasa/rasa/core/training/interactive.py", line 312, in _ask_questions
should_retry = await _ask_if_quit(sender_id, endpoint)
File "/Users/erohmensing/rasa/rasa/core/training/interactive.py", line 591, in _ask_if_quit
await _write_data_to_file(sender_id, endpoint)
File "/Users/erohmensing/rasa/rasa/core/training/interactive.py", line 566, in _write_data_to_file
await _write_stories_to_file(story_path, events)
File "/Users/erohmensing/rasa/rasa/core/training/interactive.py", line 764, in _write_stories_to_file
with open(export_story_path, append_write, encoding="utf-8") as f:
FileNotFoundError: [Errno 2] No such file or directory: ''
2019-06-18 08:16:35 ERROR    asyncio  - Task exception was never retrieved
future: <Task finished coro=<_serve_application.<locals>.run_interactive_io() done, defined at /Users/erohmensing/rasa/rasa/core/training/interactive.py:1409> exception=FileNotFoundError(2, 'No such file or directory')>
Traceback (most recent call last):
File "/Users/erohmensing/rasa/rasa/core/training/interactive.py", line 1416, in run_interactive_io
sender_id=uuid.uuid4().hex,
File "/Users/erohmensing/rasa/rasa/core/training/interactive.py", line 1367, in record_messages
endpoint, sender_id, sender_ids, plot_file
File "/Users/erohmensing/rasa/rasa/core/training/interactive.py", line 885, in _predict_till_next_listen
action_name, policy, confidence, predictions, endpoint, sender_id
File "/Users/erohmensing/rasa/rasa/core/training/interactive.py", line 1024, in _validate_action
is_correct = await _ask_questions(question, sender_id, endpoint)
File "/Users/erohmensing/rasa/rasa/core/training/interactive.py", line 312, in _ask_questions
should_retry = await _ask_if_quit(sender_id, endpoint)
File "/Users/erohmensing/rasa/rasa/core/training/interactive.py", line 591, in _ask_if_quit
await _write_data_to_file(sender_id, endpoint)
File "/Users/erohmensing/rasa/rasa/core/training/interactive.py", line 566, in _write_data_to_file
await _write_stories_to_file(story_path, events)
File "/Users/erohmensing/rasa/rasa/core/training/interactive.py", line 764, in _write_stories_to_file
with open(export_story_path, append_write, encoding="utf-8") as f:
FileNotFoundError: [Errno 2] No such file or directory: ''

# Fixed code
async def _write_nlu_to_file(
    export_nlu_path: Text, events: List[Dict[Text, Any]]
) -> None:
    """Write the nlu data of the sender_id to the file paths."""
    from rasa.nlu.training_data import TrainingData

    msgs = _collect_messages(events)

    # noinspection PyBroadException
    try:
        previous_examples = loading.load_data(export_nlu_path)
    except Exception as e:
        logger.debug(
            "An exception occurred while trying to load the NLU data. {}".format(str(e))
        )
        # No previous file exists, use empty training data as replacement.
        previous_examples = TrainingData()

    nlu_data = previous_examples.merge(TrainingData(msgs))

    # need to guess the format of the file before opening it to avoid a read
    # in a write
    if loading.guess_format(export_nlu_path) in {"md", "unk"}:
        fformat = "md"
    else:
        fformat = "json"

    with open(export_nlu_path, "w", encoding="utf-8") as f:
        if fformat == "md":
            f.write(nlu_data.as_markdown())
        else:
            f.write(nlu_data.as_json())