【发布时间】: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