I want to make a virtual file system from a few folders and want to check if there are any conflicting files. So I want to provide a few folders and get files with the same path relative to their folders.

How can I find the conflicts?

This is what I’ve done so far. The get_files and remove_duplicates functions aren’t working as I expected.

import os
import shutil
import sys

from collections import Counter
from pathlib import Path
from typing import List


def main():
    folders = sys.argv[1:]

    if len(folders) < 2:
        print("Please provide at least 2 folders")
        exit(1)

    files = get_files(folders)
    conflicting_files = find_conflicting_files(files)
    conflicting_files = remove_duplicates(conflicting_files)
    print_conflicting_files(conflicting_files)


def get_files(folders):
    files = []
    for folder in folders:
        files.extend([os.path.relpath(path, folder) for path in Path(folder).rglob("*")])
    return files


def test_get_files():
    try:
        os.makedirs("test/folder1/a", exist_ok=True)
        os.makedirs("test/folder2/b", exist_ok=True)
        open("test/folder1/a/file", "w").close()
        open("test/folder2/b/file", "w").close()

        folders = ["test/folder1", "test/folder2"]
        assert get_files(folders) == ["a/file", "b/file"]
    finally:
        shutil.rmtree("test")


def find_conflicting_files(files) -> List:
    return [file for file, cnt in Counter(files).items() if cnt > 1]


def test_find_conflicting_files():
    files = [
        ["a", "b", "c"],
        ["a", "b", "d"],
        ["a", "b", "e"],
        ["a", "b", "f"],
    ]

    assert find_conflicting_files(files) == ["a", "a", "a", "b", "b", "b"]


def remove_duplicates(l: List) -> List:
    return [*set(l)]


def test_remove_duplicates():
    files = ["a", "a", "b", "b", "c", "c"]
    assert remove_duplicates(files) == ["a", "b", "c"]


def print_conflicting_files(files):
    for file in files:
        print(file)


if __name__ == "__main__":
    main()