【发布时间】:2018-07-25 15:31:30
【问题描述】:
我有一个 python 脚本,我正在尝试读取目录中的所有 .txt 文件,并确定它们是否针对我脚本中的任何条件返回 True 或 False。我没有收到错误消息,但脚本没有产生任何输出。我希望脚本读取包含格式为 .json 格式的文本的 .txt 文件。然后我希望脚本确定 .txt 文件是否与下面我的代码中的任何语句匹配。然后我想将结果输出到 csv 文件。非常感谢您的帮助!
#!/usr/bin/env python
# regarding whether any positive results were found for the domain on VT.
import csv
import json
import pprint
import sys
import os
CSVPATH = 'CsvResults.csv'
VTOUTPUTPATH = './output/'
VTOUTPUTEXT = '.txt'
#files_to_search = [f for f in os.listdir('./output/') if f[-4:] == '.txt']
#vt_result_path = files_to_search
#vt_result = vt_result_check(vt_result_path)
pp = pprint.PrettyPrinter(indent=4)
# Check files from VirusTotal queries for any positive results
# Result is false unless any nonzero positive result is true
def vt_result_check(vt_result_path):
vt_result = None
try:
vt_result = False
for filename in os.listdir(path):
with open(filename, 'r', encoding='utf-16') as vt_result_file:
vt_data = json.load(vt_result_file)
#vt_result_path = [f for f in os.listdir('./output/') if f[-4:] == '.txt']
#vt_result = None
#try:
# vt_result = False
# with open(infile) as vt_result_file:
# vt_data = json.load(vt_result_file)
# Look for any positive detected referrer samples
try:
for sample in (vt_data['detected_referrer_samples']):
if (sample['positives'] > 0):
vt_result = True
except:
pass
# Look for any positive detected communicating samples
try:
for sample in (vt_data['detected_communicating_samples']):
if (sample['positives'] > 0):
vt_result = True
except:
pass
# Look for any positive detected downloaded samples
try:
for sample in (vt_data['detected_downloaded_samples']):
if (sample['positives'] > 0):
vt_result = True
except:
pass
# Look for any positive detected URLs
try:
for sample in (vt_data['detected_urls']):
if (sample['positives'] > 0):
vt_result = True
except:
pass
# Look for a Dr. Web category of known infection source
try:
if (vt_data['Dr.Web category'] == "known infection source"):
vt_result = True
except:
pass
# Look for a Forecepoint ThreatSeeker category of elevated exposure
try:
if (vt_data['Forcepoint ThreatSeeker category'] == "elevated exposure"):
vt_result = True
except:
pass
# Look for a Forecepoint ThreatSeeker category of phishing and other frauds
try:
if (vt_data['Forcepoint ThreatSeeker category'] == "phishing and other frauds"):
vt_result = True
except:
pass
# Look for a Forecepoint ThreatSeeker category of suspicious content
try:
if (vt_data['Forcepoint ThreatSeeker category'] == "suspicious content"):
vt_result = True
except:
pass
#pp.pprint(vt_data)
except:
pass
return vt_result
def cert_check(csvpath):
with open(csvpath, 'w') as csvfile:
fieldnames = ['vt_result']
writer = csv.writer(csvfile)
writer.writerow(['VirusTotal Results'])
vt_result_path = VTOUTPUTPATH + subject_dom + VTOUTPUTEXT
vt_result = vt_result_check(vt_result_file)
writer.writerow([vt_result])
【问题讨论】:
-
你做过调试吗?我们很难遍历一个程序,因为我们必须制作带有名称的示例文件 - 或者通读整个程序,看看我们是否可以在不运行代码的情况下看到问题
-
脚本除了定义两个函数什么都不做。你应该运行它们。
-
@KlausD。那我怎么让它工作呢?非常感谢您的帮助?
-
@chevybow 我尝试进行一些调试,但没有收到任何错误消息。你知道该怎么做吗?
-
那些没用的
try/except: pass是怎么回事? 整个函数体周围甚至还有一个!您不会看到任何异常,只是想知道您的代码有什么问题。
标签: python python-3.x