python script:
statistic_row_number.py
import os
def count_lines_in_v_files(root_folder):
total_lines = 0
#遍历指定根文件夹下的所有子文件夹和文件
for foldername, subfolders, filenames in os.walk(root_folder):
for filename in filenames:
#对于每个 .v 或.vhd文件,打开文件并使用 readlines 函数来读取文件的所有行,
#并将行数累加到 total_lines 变量中
#注意:更改.v或者.vhd后缀即可实现对其他文件格式的支持
if filename.endswith((".v", ".vhd")):
file_path = os.path.join(foldername, filename)
with open(file_path, 'r', errors='ignore') as file:
lines = file.readlines()
total_lines += len(lines)
return total_lines
if __name__ == "__main__":
#注意:根文件夹目录
root_folder_path = "./prj_root"
total_lines = count_lines_in_v_files(root_folder_path)
#打印统计完成的行数
print(f"Total lines in .v and .vhd files: {total_lines}")
例子:
1. 根目录和各级子目录树:
--prj_root
--abc1
--dut1
ffn.v
ffn.v
--abc2
--dut1
ffn.v
ffn.v
其中,fftn.v代码行数为8
2. 运行statistic_row_number.py