简体   繁体   English

如何从多个文件夹中获取具有相同相对路径的冲突文件?

[英]How to get conflicting files with same relative paths from multiple folders?

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 aren't working as I expected. get_filesremove_duplicates没有按我的预期工作。

from glob import glob
import os
from pathlib import Path
import shutil
import sys


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:
    conflicting_files = []
    for file in files[0]:
        for f in files[1:]:
            if file in f:
                conflicting_files.append(file)

    return conflicting_files


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(files):
    return list(set(files))


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()

Try (haven't tested it myself):尝试(我自己没有测试过):

def find_conflicting_files(files) -> list:
    from collections import Counter
    return [file for file, cnt in Counter(files).items() if cnt > 1) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何比较两个文件夹、子文件夹和文件的相对路径并将所有匹配文件的绝对路径传递给函数? - How to compare the relative paths two folders, subforlders and file and pass the absolute path of all the matching files to a function? 如何使用 Pathlib 获取相对路径 - How to get relative paths with Pathlib 如何从 zip 文件的多个文件夹访问多个 CSV 文件 - How to access multiple CSV files that share the same name from multiple folders from a zip file 如何从python中的多个文件夹读取文件 - how to read files from multiple folders in python 如何使用python从位于同一目录中的多个zip文件夹中读取csv文件? - How to read csv files from multiple zip folders located in the same directory using python? Python Selenium:如何从具有相同相对 xpath 的未定义长度的多个 html 列表中获取元素? - Python Selenium: How to get elements from multiple html lists of undefined length that have the same relative xpath? 如何组合 Python 中多个文件夹中的多个 CSV 文件? - how to combine multiple CSV files from multiple folders in Python? ¿如何在 Python 中从多个文件夹中读取多个文件 - ¿How do I read multiple files from multiple folders in Python 如何通过Python合并来自同名但位于不同文件夹中的文件的内容? - How to merge content from files with same name but in different folders by Python? 如何从多个文件夹和文件中读取特定段落 - How to read a specific paragraph from from multiple folders and files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM