使用我的python脚本获取多个错误消息

时间:2018-07-25 19:46:46

标签: python json python-3.x path python-3.6

我有一个python脚本,在其中尝试读取目录中的所有.txt文件,并确定它们是否针对我脚本中的任何条件返回True或False。我收到以下列出的一些错误消息。我希望脚本读取包含以.json格式格式化的文本的.txt文件。然后,我希望脚本确定.txt文件是否与下面我的代码中的任何语句匹配。然后,我想将结果输出到一个csv文件。非常感激你的帮助!第二条错误消息没有任何意义,因为我在路径中指定的目录中存在.json格式的.txt文件。

npm install

再次出现错误消息*

File "C:/Users/xxx/Documents/best_version_of_vt_checker.py", line 34, in <module>
    print(vt_result_check(path))
  File "C:/Users/xxx/Documents/best_version_of_vt_checker.py", line 10, in vt_result_check
    with open(filename, 'r') as vt_result_file:
FileNotFoundError: [Errno 2] No such file or directory: '.2500.c.dynadns.eu.txt'

import os
import json

path=r'./output/'


def vt_result_check(path):
    vt_result = False
    for filename in os.listdir(path):
        with open(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

    return vt_result

if __name__ == "__main__":
    print(vt_result_check(path))
    with open(csvpath, 'w') as csvfile:
        writer.writerow([vt_result_check()])

1 个答案:

答案 0 :(得分:0)

您正在使用os.listdir来获取文件夹output中的所有文件。但是,当您尝试打开它们时,仅使用文件名,而不使用output文件夹。您需要将路径以及文件名包括在内。

用以下行替换行with open(filename, 'r') as vt_result_file:with open(path+filename, 'r') as vt_result_file:

这将路径和文件名结合在一起,以便可以正确找到文件。