Since the file path can be of an arbitrary depth, we need something scalable. 
Here is a recursive approach - splitting the path recursively until we get to the root /:
import os
paths = [
    ("/test/file1.txt", "content1"),
    ("/test/file2.txt", "content2"),
    ("/file3.txt", "content3"),
    ("/test1/test2/test3/file5.txt", "content5"),
    ("/test2/file4.txt", "content4")
]
def deepupdate(original, update):
    for key, value in original.items():
        if key not in update:
            update[key] = value
        elif isinstance(value, dict):
            deepupdate(value, update[key])
    return update
def traverse(key, value):
    directory = os.path.dirname(key)
    filename = os.path.basename(key)
    if directory == "/":
        return value if isinstance(value, dict) else {filename: value}
    else:
        path, directory = os.path.split(directory)
        return traverse(path, {directory: {filename: value}})
result = {}
for key, value in paths:
    result = deepupdate(result, traverse(key, value))
print(result)
Using deepupdate() function suggested here.
It prints:
{'file3.txt': 'content3',
 'test': {'file1.txt': 'content1', 'file2.txt': 'content2'},
 'test1': {'test2': {'test3': {'file5.txt': 'content5'}}},
 'test2': {'file4.txt': 'content4'}}