from pathlib import Path


def path_to_root(root, target):
    """
    Given a root path and a target path that exists under the root
    folder somewhere, return the number of relative directory up
    commands (i.e. '../my.file') to use to get to the root folder.


    # Parameters

    root:pathlib.Path
        - the root folder we want to change directories too.

    target:pathlib.Path
        - The target folder/file that we want to see how many steps to get to root from here.
        - It must be a sub-path of root otherwise an exception is raised.

    # Return

    A Path object representing the relative number of directory changes
    to get to the root folder from the target folder.

    # Example

    root = /folder1/folder2/folder3

    target = /folder1/folder2/folder3/1/2/3/file.md

    result = ../../../

    """

    count = len(target.relative_to(root).parents) - 1

    if count == 0:
        return Path(".")

    else:
        return Path("/".join([".."] * count) + "/")