当Python脚本应该为100000行生成True或False时,它会生成空的.csv文件作为输出

2024-04-20 12:15:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python脚本,它应该获取一个满是.txt文件的目录,并确定每个.txt文件是否返回正数或负数,以匹配文件本身中的某些文本语句,如“已知感染源”。但是,我的脚本运行了大约15秒,然后说它完成了。然而输出.csv脚本创建的文件为空。该.csv文件应包含真假约100000.txt文件任何帮助将不胜感激!你知道吗

代码

import os
import json
import csv

path="C:/Users/bwerner/Documents/output/"


def vt_result_check(path):
    vt_result = False
    for filename in os.listdir(path):
        with open(path + filename, 'r') as vt_result_file:
            vt_data = json.load(vt_result_file)

        # Look for any positive detected referrer samples
        # Look for any positive detected communicating samples
        # Look for any positive detected downloaded samples
        # Look for any positive detected URLs
        sample_types = ('detected_referrer_samples', 'detected_communicating_samples',
                        'detected_downloaded_samples', 'detected_urls')
        vt_result |= any(sample['positives'] > 0 for sample_type in sample_types
                                                 for sample in vt_data.get(sample_type, []))

        # Look for a Dr. Web category of known infection source
        vt_result |= vt_data.get('Dr.Web category') == "known infection source"

        # Look for a Forecepoint ThreatSeeker category of elevated exposure
        # Look for a Forecepoint ThreatSeeker category of phishing and other frauds
        # Look for a Forecepoint ThreatSeeker category of suspicious content
        threats = ("elevated exposure", "phishing and other frauds", "suspicious content")
        vt_result |= vt_data.get('Forcepoint ThreatSeeker category') in threats

        vt_result = int(vt_result)

        return vt_result

if __name__ == '__main__':
    for i in range(vt_result_check(path)):
        f = csv.writer(open("output.csv", "wb+"))
        print(vt_result_check(path))
        f.writerow(vt_result_check(path))

Tags: 文件csvsamplepathinfordatacheck