from pathlib import Path


def find_folders(master_folder):
    """
    Search for folders inside the given address.

    Parameters
    ----------
    master_folder : string
        address of the master folder on local computer.

    Raises
    ------
    AttributeError:
        Raises error when no folder can be found.

    Returns
    -------
    measurements : list
        list with the address of folders inside master_folder.
    """
    master_string = master_folder
    folder_master = Path(master_string)

    if not folder_master.exists():
        raise AttributeError("Folder could not be found.")

    # initialise lists to account for measurement folders and registers

    measurements = []

    # detect the measurement folders where each of the data runnings
    # are stored

    for meas_folder in (folder_master).glob('*'):
        if meas_folder.is_dir():
            measurements.append(meas_folder)
    if len(measurements) == 0:
        print("No folders were found.")
    else:
        print("In the selected path {} measurement folders were found."
              .format(len(measurements)))
        print("Choose the folder to be analysed using its index.")
        for counter, path in enumerate(measurements):
            print("Option {}: \n {}".format(counter, path))

    return measurements