Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-名称相同但内容不同的文件_Python_File_Diff - Fatal编程技术网

Python-名称相同但内容不同的文件

Python-名称相同但内容不同的文件,python,file,diff,Python,File,Diff,我有两个文件夹,dir1和dir2。我必须找到两个文件夹(或子文件夹)中名称相同但内容不同的文件 类似于:so.1.0/p/q/search.cso.1.1/p/q/search.c 有什么想法吗 我通过以下方式获取所需文件: import os, sys, fnmatch, filecmp folder1 = sys.argv[1] folder2 = sys.argv[2] filelist1 = [] filelist2 = [] for root, dirs, files in

我有两个文件夹,dir1和dir2。我必须找到两个文件夹(或子文件夹)中名称相同但内容不同的文件

类似于:so.1.0/p/q/search.cso.1.1/p/q/search.c

有什么想法吗

我通过以下方式获取所需文件:

import os, sys, fnmatch, filecmp

folder1 = sys.argv[1]
folder2 = sys.argv[2]

filelist1 = []

filelist2 = []

for root, dirs, files in os.walk(folder1):
    for filename in fnmatch.filter(files, '*.c'):
         filelist1.append(os.path.join(root, filename))

for root, dirs, files, in os.walk(folder1):
    for filename in fnmatch.filter(files, '*.h'):
        filelist1.append(os.path.join(root, filename))

for root, dirs, files in os.walk(folder2):
    for filename in fnmatch.filter(files, '*.c'):
         filelist2.append(os.path.join(root, filename))

for root, dirs, files, in os.walk(folder2):
    for filename in fnmatch.filter(files, '*.h'):
        filelist2.append(os.path.join(root, filename))
现在,我想比较两个文件列表,获取具有相同文件名的条目,并检查它们的内容是否不同。您认为如何?

用于生成任一目录中的文件列表(具有相对于其根的路径):

从以下路径之一创建一组路径:

root_one = 'so.1.0'  # use an absolute path here
root_two = 'so.1.1'  # use an absolute path here
files_one = set(relative_files(root_one))
然后使用集合交叉点查找其他根目录中相同的所有路径名:

from itertools import izip_longest

def different_files(root_one, root_two):
    """Yield files that differ between the two roots

    Generate pathnames relative to root_one and root_two that are present in both
    but have different contents.

    """
    files_one = set(relative_files(root_one))
    for same in files_one.intersection(relative_files(root_two)):
        # same is a relative path, so same file in different roots
        with open(os.path.join(root_one, same)) as f1, open(os.path.join(root_two, same)) as f2:
            if any(line1 != line2 for line1, line2 in izip_longest(f1, f2)):
                # lines don't match, so files don't match! 
                yield same
在文件上循环有效地配对行;如果一个文件比另一个文件长,则剩余的行将与
None
配对,以确保检测到一个文件与另一个文件不同

演示:


由于@Martijn回答了遍历的目的,您可以使用
os.walk()

对于文件名比较,我建议

>>> import filecmp
>>> filecmp.cmp('undoc.rst', 'undoc.rst') 
True
>>> filecmp.cmp('undoc.rst', 'index.rst') 
False
用于比较文件内容签出

>>> import filecmp
>>> filecmp.cmp('undoc.rst', 'undoc.rst') 
True
>>> filecmp.cmp('undoc.rst', 'index.rst') 
False
for root, dirs, files in os.walk(path):
    for name in files:
>>> import filecmp
>>> filecmp.cmp('undoc.rst', 'undoc.rst') 
True
>>> filecmp.cmp('undoc.rst', 'index.rst') 
False