import os
import openpyxl
# 指定要读取的主文件夹路径
main_folder_path = '//'
# 创建一个新的Excel工作簿和工作表
workbook = openpyxl.Workbook()
sheet = workbook.active
# 添加表头
sheet['A1'] = '列名'
sheet['B1'] = '列名'
sheet['C1'] = '列名'
# 创建一个空列表来存储行号数据
line_numbers = []
def find_txt_files(folder):
txt_files = []
# 获取文件夹中的所有文件和子文件夹
items = os.listdir(folder)
for item in items:
item_path = os.path.join(folder, item)
if os.path.isfile(item_path) and item.endswith('.txt'):
# 如果是txt文件,将其添加到列表中
txt_files.append(item_path)
elif os.path.isdir(item_path):
# 如果是子文件夹,递归查找其中的txt文件
txt_files.extend(find_txt_files(item_path))
return txt_files
# 确保主文件夹存在
if not os.path.exists(main_folder_path):
print(f"主文件夹 '{main_folder_path}' 不存在。")
else:
# 查找所有txt文件
txt_files = find_txt_files(main_folder_path)
# # 查找所有sol文件
# sol_files = find_sol_files(main_folder_path)
if not txt_files:
print(f"主文件夹 '{main_folder_path}' 及其子文件夹中没有txt文件。")
else:
# 将.txt文件的内容和文件路径写入Excel表格
for i, txt_file in enumerate(txt_files):
with open(txt_file, 'r', encoding='utf-8') as file:
file_contents = file.read()
# 获取文件名和文件路径
file_name = os.path.basename(txt_file)
file_path = os.path.abspath(txt_file)
# 使用os.path.split函数来分割文件路径,然后去掉前五级目录
_, _, remaining_path = file_path.partition(os.path.sep)
_, _, remaining_path = remaining_path.partition(os.path.sep)
_, _, remaining_path = remaining_path.partition(os.path.sep)
_, _, remaining_path = remaining_path.partition(os.path.sep)
_, _, remaining_path = remaining_path.partition(os.path.sep)
# 去掉最后一级目录
remaining_path, _ = os.path.split(remaining_path)
#处理漏洞名称
file_name=file_name.split(".")[0]
contract_name=file_name.split("_")[0]
file_name=file_name.split("_")[1]
file_name=file_name[:-4]
file_path = os.path.join(remaining_path, contract_name+'_'+file_name +'.sol')
sheet.cell(row=i + 2, column=1, value=file_path)
#处理漏洞行数
# 提取行号数据并添加到列表中
lines = file_contents.split('\n')
for line in lines:
# 如果行包含"line_number:",则提取行号并添加到列表中
if "line_number:" in line:
line_number = int(line.split(":")[1].strip())
line_numbers.append(line_number)
sheet.cell(row=i + 2, column=3, value=', '.join(map(str, line_numbers)))
# 清空行号数据列表以处理下一个文件
line_numbers = []
# 将漏洞名、行数写入Excel表格的不同列
sheet.cell(row=i + 2, column=2, value=file_name)
# 保存Excel文件
excel_file_path = 'output1.xlsx'
workbook.save(excel_file_path)
print(f"已将数据保存到 '{excel_file_path}' 中。")
python读取文件夹内容写入Excel
最新推荐文章于 2024-07-25 13:53:29 发布