猿问

Python脚本未产生任何输出

我有一个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


   

    

qq_笑_17
浏览 188回答 2
2回答

jeck猫

您实际上需要将这些函数称为我的兄弟def my_func(stuff):    print(stuff) #or whatevermy_func(1234)每条评论更新import osp=r'path\to\your\files' filelist=os.listdir(p) #creates list of all files/folders in this dir#make a loop for each file in the dirfor file in filelist:    f=os.path.join(p,file) #this just joins the file name and path for full file path    your_func(f)  #here you can pass the full file name to your functions

慕村225694

如前所述,当前的问题似乎是您根本不调用cert_check函数。但是,尽管此站点实际上不用于代码审查,但我不禁建议对您的代码进行一些改进。特别是,所有这些try/except:pass构造都使得检测代码中的任何错误变得异常困难,因为所有异常只会被静默捕获和吞噬except: pass。您应该删除所有这些try/except:pass块,尤其是围绕整个功能主体的那个块如果某些键不存在,则可以使用dict.get代替代替[],这不会引发键错误,而是返回None(或一些默认值),并且所有检查仍然可以进行您可以使用|=而不是if检查来or检查变量的结果您可以any用来检查某个列表中的任何元素是否满足某些条件我的vt_result_check函数版本:def vt_result_check(vt_result_path):    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)        # 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
随时随地看视频慕课网APP

相关分类

Python
我要回答