我有一个python脚本,我试图读取目录中的所有.txt文件,并确定它们在脚本中的任何条件下是否返回True或False。我收到了下面列出的一些错误消息。我希望脚本在.txt文件中读取,这些文件包含以.json格式格式化的文本。然后,我希望脚本确定.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:UsersbwernerAppDataLocalContinuumanaconda3Libjson__init__.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:UsersbwernerAppDataLocalContinuumanaconda3Libjson__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:UsersbwernerAppDataLocalContinuumanaconda3Libjsondecoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:UsersbwernerAppDataLocalContinuumanaconda3Libjsondecoder.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)
您正在使用os.listdir获取文件夹output
中的所有文件。但是,当您尝试打开它们时,您只使用文件名,而没有使用output
文件夹。您需要包括路径和文件名。
将线路with open(filename, 'r') as vt_result_file:
替换为以下线路:with open(path+filename, 'r') as vt_result_file:
这将合并路径和文件名,以便正确定位文件。