需要一个python脚本,它在一个目录中输出具有相同内容的文件列表

2024-03-28 12:12:01 发布

您现在位置:Python中文网/ 问答频道 /正文

给定一个包含几千个文件的目录,请输出所有 目录中完全相同的文件

我可以编写脚本从dir读取文件,但是我需要比较目录中每个文件的内容,并且输出应该像matches{f1,f2}{f4,f6}

#!/usr/bin/env python

import sys
import glob
import errno

path = '/home/harish/myfiles'   
files = glob.glob(path)   
for name in files:  # 'file' is a builtin type, 'name' is a less-ambiguous variable name.
    try:
        with open(name) as f: # No need to specify 'r': this is the default.
            sys.stdout.write(f.read())
    except IOError as exc:
        if exc.errno != errno.EISDIR: # Do not fail if a directory is found, just ignore it.
            raise # Propagate other kinds of IOError.

Tags: 文件pathnameimport目录脚本ifis