【问题标题】:Getting multiple error messages with my python script使用我的 python 脚本获取多条错误消息
【发布时间】:2018-07-25 19:46:46
【问题描述】:

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

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

再次出现错误消息*

File "C:/Users/bwerner/Documents/3pm_reporter.py", line 34, in <module>
    print(vt_result_check(path))
  File "C:/Users/bwerner/Documents/3pm_reporter.py", line 11, in vt_result_check
    vt_data = json.load(vt_result_file)
  File "C:\Users\bwerner\AppData\Local\Continuum\anaconda3\Lib\json\__init__.py", line 299, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "C:\Users\bwerner\AppData\Local\Continuum\anaconda3\Lib\json\__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "C:\Users\bwerner\AppData\Local\Continuum\anaconda3\Lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\bwerner\AppData\Local\Continuum\anaconda3\Lib\json\decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

【问题讨论】:

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


    【解决方案1】:

    您正在使用 os.listdir 来获取文件夹 output 中的所有文件。但是,当您尝试打开它们时,您只使用了文件名,没有 output 文件夹。您需要包含路径和文件名。

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

    这结合了路径和文件名,以便正确定位文件。

    【讨论】:

    • 感谢您的帮助。它已经运行了最后几分钟,所以我不知道输出是什么,因为它没有向控制台打印任何内容。我想将输出打印到 csv。我的代码是: with open(csvpath, 'w') as csvfile: writer.writerow([vt_result_check()]) 这是对的吗?感谢您的所有帮助!
    • @bedford 您的问题是关于修复该错误。如果您还有其他问题,请将其发布为new question。如果我上面的回答解决了您发布的问题,请点击左侧的复选标记接受。
    • 我实际上又遇到了同样的错误,因为脚本最终失败了。我包含了上面的错误消息。
    • @bedford 这是一条不同的错误消息,似乎是因为 JSON 文件无效。如果您还有其他问题,请再次提出新问题。
    猜你喜欢
    • 2020-12-06
    • 1970-01-01
    • 2020-06-24
    • 2022-01-21
    • 2019-01-08
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多