Group duplicate files
As far as I can tell, your algorithm is something like \$\mathcal{O}(n!)\$, because you compare all possible pairs of files. It is possible to do this in \$\mathcal{O}(n)\$.

You just have to iterate over the files and order them into a hashmap (i.e. dictionary) based on their header hash. Then, resolve the possible conflicts of two files with the same header, but different content, which takes \$\mathcal{O}(k)\$, where \$k\$ is the number of files with the same header. So in the worst case (all files have the same header), this algorithm is \$\mathcal{O}(2n)\$.

from collections import defaultdict
import sys


def hash_content(file_name, length=-1):
    with open(file_name) as f:
        return hash(f.read(length))


def group_by_header(*file_names):
    file_groups = defaultdict(list)
    for file_name in file_names:
        file_groups[hash_content(file_name, length=10)].append(file_name)
    return file_groups


def group_by_content(*file_names):
    file_groups = group_by_header(*file_names)
    for file_names in file_groups.values():
        if len(file_names) &gt; 1:
            while file_names:
                file_name = file_names.pop()
                file_groups[hash_content(file_name)].append(file_name)
    return filter(None, file_groups.values())


if __name__ == "__main__":
    file_names = sys.argv[1:]
    print group_by_content(*file_names)


This code has two caveats:


It assumes hash is collision-free (a fair assumption). We could pass the string content directly as a key and let python do the hashing. Then we would also get a strategy for the rare case that two strings have the same hash without being equal. But this way we avoid storing the possibly large file-contents as strings.
This uses the fact that dict.values() returns a list and not an iterator, so we can modify the dictionary in the loop here. This breaks in Python 3.x, but you specifically tagged this as Python 2.7. You can remedy this by introducing a new dictionary:




def group_by_content(*file_names):
    header_hashs = group_by_header(*file_names)
    content_hashs = defaultdict(list)
    for hash_, file_names in header_hashs.items():
        if len(file_names) &gt; 1:
            while len(file_names):
                file_name = file_names.pop()
                content_hashs[hash_content(file_name)].append(file_name)
        else:
            content_hashs[hash_] = file_names
    return (val for val in content_hashs.values() if val)