import networkx as nx import os from tqdm import tqdm import csv #用于直接读取gexf信息保存的代码 def write_csv(CFG_dir = "../CFG_data/malware",csv_save_path="../CFG_data/malware_msg.csv",header = ['malware_name','nodes_num','edgs_num']): file_list=os.listdir(CFG_dir)[:10000] # benign_list=os.listdir(bengin_CFG_dir) csv_data = [] for item_name in tqdm(file_list): file=os.path.join(CFG_dir,item_name) G=nx.read_gexf(file) nodes_num = len(G.nodes()) edgs_num = len(G.edges()) csv_data.append([item_name, str(nodes_num), str(edgs_num)]) with open(csv_save_path,'w',encoding='utf-8',newline='') as fp: writer =csv.writer(fp) writer.writerow(header) writer.writerows(csv_data) print("成功结束") if __name__ == '__main__': write_csv(CFG_dir="../cfg_data_with_feature/benign1", csv_save_path="../cfg_data_with_feature/benign_msg1.csv",header=['benign_name', 'nodes_num', 'edgs_num','insert_point_count']) write_csv(CFG_dir="../cfg_data_with_feature/malware1", csv_save_path="../cfg_data_with_feature/malware_msg1.csv",header=['malware_name', 'nodes_num', 'edgs_num', 'insert_point_count'])