Python脚本:遍历根文件夹下的所有子文件夹,查找文件名为“common_ctrl.v“的文件,并把文件的路径及文件名,打印出来。然后把找到的多个common_ctrl.v文件内容不同的地方列出来。

python script:

find_and_compare.py

import os
import difflib

def find_and_compare(root_folder, target_filename, output_file):
    # 用于存储文件路径及文件名
    file_paths = []
    
    # 用于存储 common_ctrl.v 文件的内容
    file_contents = {}

    # 用于记录已经比较过的文件对
    compared_files = set()

    # 遍历文件夹
    for foldername, subfolders, filenames in os.walk(root_folder):
        for filename in filenames:
            if filename == target_filename:
                file_path = os.path.join(foldername, filename)
                file_paths.append(file_path)

    # 打印文件路径及文件名,并存储到文件
    with open(output_file, 'w') as output:
        output.write("Files with name '{}':\n".format(target_filename))
        for file_path in file_paths:
            print(file_path)
            print(file_path, file=output)

        # 读取 common_ctrl.v 文件的内容
        for file_path in file_paths:
            with open(file_path, 'r', encoding='utf-8', errors='ignore') as file:
                file_contents[file_path] = file.readlines()

        # 比较文件内容并列出不同之处,并存储到文件
        for file_path1 in file_paths:
            for file_path2 in file_paths:
                if file_path1 != file_path2 and (file_path1, file_path2) not in compared_files and (file_path2, file_path1) not in compared_files:
                    compared_files.add((file_path1, file_path2))
                    compared_files.add((file_path2, file_path1))

                    output.write("\nDifferences between '{}' and '{}':\n".format(file_path1, file_path2))
                    d = difflib.Differ()
                    diff = list(d.compare(file_contents[file_path1], file_contents[file_path2]))
                    
                    identical = True
                    for line_num, line in enumerate(diff, start=1):
                        if line.startswith('- ') or line.startswith('+ '):
                            identical = False
                            output.write("Line {}: {}\n".format(line_num, line.strip()))
                            print("Line {}: {}".format(line_num, line.strip()))

                    if identical:
                        output.write("identical\n")
                        print("identical")


# 设置根文件夹路径和目标文件名
root_folder = "./"
target_filename = "common_ctrl.v"
output_file = "comparison_result.txt"

# 调用函数进行查找并比较
find_and_compare(root_folder, target_filename, output_file)

例子:

1. common_ctrl.v

module ffn (
  input  [11:0] gopt_i,
  input  [11:0] gopt_q,
  input  [11:0] half_i,
  input  [11:0] half_q,
  output [13:0] helt_i,
  output [11:0] helt_q,
  output [13:0] hppt_i,
  output [11:0] hppt_q
 );

 assign hppt_i[0] = helt_i[0];
 assign hppt_q[0] = helt_q[1];

2 & 3. common_ctrl.v

module ffn (
  input  [11:0] gopt_i,
  input  [11:0] gopt_q,
  input  [11:0] half_i,
  input  [11:0] half_q,
  output [13:0] helt_i,
  output [13:0] helt_q,
  output [13:0] hppt_i,
  output [13:0] hppt_q
 );

 assign hppt_i[0] = helt_i[0];
 assign hppt_q[0] = helt_q[0];

运行python find_and_compare.py 查看结果:

terminal 打印:

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值