首页 > 解决方案 > 当 Python 脚本应该为 100,000 行生成 True 或 False 时,它​​会生成空的 .csv 文件作为输出

问题描述

我有一个 python 脚本,它应该获取一个充满 .txt 文件的目录,并确定每个 .txt 文件是否返回正数或负数以匹配文件本身内的某些文本语句,例如“已知感染源”。但是,我的脚本运行了大约 15 秒,然后说它完成了。但是,脚本创建的 output.csv 文件是空的。.csv 文件应包含大约 100,000 个 .txt 文件的 true 和 false 任何帮助将不胜感激!

代码

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))

标签: pythonjsonpython-3.x

解决方案


推荐阅读