I have a python script where I'm trying to read all .txt files in a directory and determine if they return True or False for any conditions that are in my script. I don't get an error message but the script doesn't produce any output. I want the script to read in .txt files that contain text formatted in .json format. Then I want the script to determine if the .txt file matches any of the statements in my code below. I then want to output the result to a csv file. Your help is very much appreciated!
#!/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])
try/except: pass? There's even one around the entire body of the function! You won't see any exceptions and just wonder what's wrong with your code.