def METHOD_NAME(fdname):
    # fdname is 'stdout' or 'stderr'
    stdfspec = kwargs.get(fdname)  # spec is str name or tuple (name, mode)
    if stdfspec is None:
        return None
    fname, mode = get_std_fname_mode(fdname, stdfspec)
    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