# Buggy code
def _get_path(path, key, name):
    """Get a dataset path."""
    # 1. Input
    if path is not None:
        if not isinstance(path, str):
            raise ValueError("path must be a string or None")
        return path
    # 2. get_config(key)
    # 3. get_config('MNE_DATA')
    path = get_config(key, get_config("MNE_DATA"))
    if path is not None:
        return path
    # 4. ~/mne_data (but use a fake home during testing so we don't
    #    unnecessarily create ~/mne_data)
    logger.info("Using default location ~/mne_data for %s..." % name)
    path = op.join(os.getenv("_MNE_FAKE_HOME_DIR", op.expanduser("~")), "mne_data")
    if not op.exists(path):
        logger.info("Creating ~/mne_data")
        try:
            os.mkdir(path)
        except OSError:
            raise OSError(
                "User does not have write permissions "
                "at '%s', try giving the path as an "
                "argument to data_path() where user has "
                "write permissions, for ex:data_path"
                "('/home/xyz/me2/')" % (path)
            )
    return path


# Traceback (FileNotFoundError)
Downloading archive MNE-sample-data-processed.tar.gz to /Users/clemens/mne_data
Downloading https://files.osf.io/v1/resources/rxvq7/providers/osfstorage/59c0e26f9ad5a1025c4ab159?version=5&amp;action=download&amp;direct (1.54 GB)
0%|          | Downloading : 0.00/1.54G [00:00<?,        ?B/s]Error while fetching file https://osf.io/86qa2/download?version=5. Dataset fetching aborted.
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3418, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-9a4a5c6c7639>", line 1, in <module>
runfile('/Users/clemens/Library/Application Support/JetBrains/PyCharm2020.3/scratches/scratch.py', wdir='/Users/clemens/Library/Application Support/JetBrains/PyCharm2020.3/scratches')
File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/Users/clemens/Library/Application Support/JetBrains/PyCharm2020.3/scratches/scratch.py", line 5, in <module>
data_path = sample.data_path()
File "<decorator-gen-461>", line 24, in data_path
File "/Users/clemens/Repositories/mne-python/mne/datasets/sample/sample.py", line 19, in data_path
return _data_path(path=path, force_update=force_update,
File "/Users/clemens/Repositories/mne-python/mne/datasets/utils.py", line 418, in _data_path
remove_archive, full = _download(path, u, an, h)
File "/Users/clemens/Repositories/mne-python/mne/datasets/utils.py", line 475, in _download
_fetch_file(url, full_name, print_destination=False,
File "<decorator-gen-3>", line 24, in _fetch_file
File "/Users/clemens/Repositories/mne-python/mne/utils/fetching.py", line 117, in _fetch_file
_get_http(url, temp_file_name, initial_size, timeout, verbose_bool)
File "/Users/clemens/Repositories/mne-python/mne/utils/fetching.py", line 55, in _get_http
with open(temp_file_name, mode) as local_file:
FileNotFoundError: [Errno 2] No such file or directory: '/Users/clemens/mne_data/MNE-sample-data-processed.tar.gz.part'

# Fixed code
def _get_path(path, key, name):
    """Get a dataset path."""
    # 1. Input
    if path is not None:
        if not isinstance(path, str):
            raise ValueError("path must be a string or None")
        return path
    # 2. get_config(key)
    # 3. get_config('MNE_DATA')
    path = get_config(key, get_config("MNE_DATA"))
    if path is not None:
        if not op.exists(path):
            msg = (
                f"Download location {path} as specified by MNE_DATA does "
                f"not exist. Either create this directory manually and try "
                f"again, or set MNE_DATA to an existing directory."
            )
            raise FileNotFoundError(msg)
        return path
    # 4. ~/mne_data (but use a fake home during testing so we don't
    #    unnecessarily create ~/mne_data)
    logger.info("Using default location ~/mne_data for %s..." % name)
    path = op.join(os.getenv("_MNE_FAKE_HOME_DIR", op.expanduser("~")), "mne_data")
    if not op.exists(path):
        logger.info("Creating ~/mne_data")
        try:
            os.mkdir(path)
        except OSError:
            raise OSError(
                "User does not have write permissions "
                "at '%s', try giving the path as an "
                "argument to data_path() where user has "
                "write permissions, for ex:data_path"
                "('/home/xyz/me2/')" % (path)
            )
    return path