# Buggy code
def open_std_fd(fdname):
    # fdname is 'stdout' or 'stderr'
    stdfspec = kwargs.get(fdname)  # spec is str name or tuple (name, mode)
    if stdfspec is None:
        return None
    elif isinstance(stdfspec, str):
        fname = stdfspec
        mode = "a+"
    elif isinstance(stdfspec, tuple):
        if len(stdfspec) != 2:
            raise pe.BadStdStreamFile(
                "std descriptor %s has incorrect tuple length %s"
                % (fdname, len(stdfspec)),
                TypeError("Bad Tuple Length"),
            )
        fname, mode = stdfspec
    else:
        raise pe.BadStdStreamFile(
            "std descriptor %s has unexpected type %s" % (fdname, str(type(stdfspec))),
            TypeError("Bad Tuple Type"),
        )

    try:
        fd = open(fname, mode)
        if os.path.dirname(fname):
            os.makedirs(os.path.dirname(fname), exist_ok=True)
    except Exception as e:
        raise pe.BadStdStreamFile(fname, e)
    return fd


# Traceback (parsl.app.errors.BadStdStreamFile)
2019-05-31 11:22:06 parsl.dataflow.dflow:251 [ERROR]  Task 0 failed
Traceback (most recent call last):
File "/home/yadu/miniconda3/envs/mpi_executor/lib/python3.6/site-packages/parsl/dataflow/dflow.py", line 248, in handle_exec_update
res.reraise()
File "/home/yadu/miniconda3/envs/mpi_executor/lib/python3.6/site-packages/parsl/app/errors.py", line 163, in reraise
reraise(t, v, tb)
File "/home/yadu/.local/lib/python3.6/site-packages/six.py", line 692, in reraise
raise value.with_traceback(tb)
File "/home/yadu/miniconda3/envs/mpi_executor/lib/python3.6/site-packages/parsl/app/errors.py", line 172, in wrapper
return func(*args, **kwargs)
File "/home/yadu/miniconda3/envs/mpi_executor/lib/python3.6/site-packages/parsl/app/bash.py", line 79, in remote_side_bash_executor
std_out = open_std_fd('stdout')
File "/home/yadu/miniconda3/envs/mpi_executor/lib/python3.6/site-packages/parsl/app/bash.py", line 76, in open_std_fd
raise pe.BadStdStreamFile(fname, e)
parsl.app.errors.BadStdStreamFile: FilePath: [/home/yadu/src/parsl/parsl/tests/manual_tests/runinfo/050/task_logs/0000/task_0000_sleeper_bash.stdout] Exception: [Errno 2] No such file or directory: '/home/yadu/src/parsl/parsl/tests/manual_tests/runinfo/050/task_logs/0000/task_0000_sleeper_bash.stdout'

# Fixed code
def open_std_fd(fdname):
    # fdname is 'stdout' or 'stderr'
    stdfspec = kwargs.get(fdname)  # spec is str name or tuple (name, mode)
    if stdfspec is None:
        return None
    elif isinstance(stdfspec, str):
        fname = stdfspec
        mode = "a+"
    elif isinstance(stdfspec, tuple):
        if len(stdfspec) != 2:
            raise pe.BadStdStreamFile(
                "std descriptor %s has incorrect tuple length %s"
                % (fdname, len(stdfspec)),
                TypeError("Bad Tuple Length"),
            )
        fname, mode = stdfspec
    else:
        raise pe.BadStdStreamFile(
            "std descriptor %s has unexpected type %s" % (fdname, str(type(stdfspec))),
            TypeError("Bad Tuple Type"),
        )

    try:
        if os.path.dirname(fname):
            os.makedirs(os.path.dirname(fname), exist_ok=True)
        fd = open(fname, mode)
    except Exception as e:
        raise pe.BadStdStreamFile(fname, e)
    return fd