<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def __write_lipd(dat, usr_path):
 """ Write LiPD data to file, provided an output directory and dataset name. :param dict dat: Metadata :param str usr_path: Destination path :param str dsn: Dataset name of one specific file to write :return none: """

    global settings
    # no path provided. start gui browse
    if not usr_path:
        # got dir path
        usr_path, _ignore = get_src_or_dst("write", "directory")
    # Check if this is a valid directory path
    valid_path = path_type(usr_path, "directory")
    # If dir path is valid
    if valid_path:
        # Filename is given, write out one file
        if "paleoData" in dat:
            try:
                if settings["verbose"]:
                    print("writing: {}".format(dat["dataSetName"]))
                lipd_write(dat, usr_path)
            except KeyError as ke:
                print("Error: Unable to write file: unknown, {}".format(ke))
            except Exception as e:
                print("Error: Unable to write file: {}, {}".format(dat["dataSetName"], e))
        # Filename is not given, write out whole library
        else:
            if dat:
                for name, lipd_dat in dat.items():
                    try:
                        if settings["verbose"]:
                            print("writing: {}".format(name))
                        lipd_write(lipd_dat, usr_path)
                    except Exception as e:
                        print("Error: Unable to write file: {}, {}".format(name, e))

    return