【问题标题】:Read all files in a directory and determine if they return True or False for any conditions读取目录中的所有文件并确定它们是否在任何条件下返回 True 或 False
【发布时间】:2018-07-25 13:59:15
【问题描述】:

我有一个 python 脚本,我正在尝试读取目录中的所有 .txt 文件,并确定它们是否针对我脚本中的任何条件返回 True 或 False。如果您向下到第 14 行,您会看到我正在尝试设置我的代码,以便它将循环并拾取所有以 .txt 结尾的文件。目录中的每个文件名都不同,因此我正在尝试实现通配符格式。非常感激您的帮忙!

#!/usr/bin/env python

import csv
import json
import pprint
import sys


CSVPATH = 'CsvResults.csv'
VTOUTPUTPATH = './output/'
VTOUTPUTEXT = '.txt'
**vt_result_path = VTOUTPUTPATH + glob.glob('dir/*') + VTOUTPUTEXT**
#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
        with open(vt_result_path) 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_path)
        writer.writerow([vt_result])

【问题讨论】:

    标签: python python-3.x wildcard


    【解决方案1】:

    您可以使用os.listdir(directoryname) 来获取目录中的文件名列表,并且您可以通过使用字符串切片和列表推导从本质上过滤该列表以查找扩展名为“.txt”的文件。例如:

    files_to_search = [f for f in os.listdir(directoryname) if f[-4:] == '.txt']
    

    将返回以“.txt”结尾的文件名列表(假设所有文件名至少有四个字符长)。

    【讨论】:

    • 感谢您的回复!如果我想将 .txt 文件作为循环读取以解析每个 .txt 文件的内容,我该怎么做?
    • 只需使用for 循环,遍历该列表。在循环内,将要执行的所有代码放在该特定文件上。例如,for file in files_to_search: ...
    猜你喜欢
    • 2013-09-14
    • 2023-04-04
    • 2015-07-28
    • 2011-11-13
    • 2011-12-02
    • 1970-01-01
    • 2019-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多