Python脚本没有生成任何输出
我有一个python脚本,试图读取目录中的所有.txt文件,并确定对于脚本中的任何条件,它们是否返回True或False。我没有收到错误消息,但脚本没有生成任何输出。我希望脚本读取包含.json格式文本的.txt文件。然后,我希望脚本确定.txt文件是否与下面代码中的任何语句匹配。然后我想将结果输出到csv文件。非常感谢你的帮助Python脚本没有生成任何输出,python,python-3.x,Python,Python 3.x,我有一个python脚本,试图读取目录中的所有.txt文件,并确定对于脚本中的任何条件,它们是否返回True或False。我没有收到错误消息,但脚本没有生成任何输出。我希望脚本读取包含.json格式文本的.txt文件。然后,我希望脚本确定.txt文件是否与下面代码中的任何语句匹配。然后我想将结果输出到csv文件。非常感谢你的帮助 #!/usr/bin/env python # regarding whether any positive results were found for the do
#!/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])
你得把这些函数称为我的兄弟
def my_func(stuff):
print(stuff) #or whatever
my_func(1234)
按评论更新
import os
p=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 dir
for 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
如前所述,当前的问题似乎是您根本就不调用
证书检查功能。然而,虽然这个站点实际上不是用于codereview的,但我还是忍不住建议对您的代码进行一些改进。特别是,所有那些try/except:pass
构造使得检测代码中的任何错误变得异常困难,因为所有异常都会被except:pass
捕获并吞没
- 您应该删除所有
try/except:pass
块,尤其是围绕整个函数体的块
- 如果某些键不存在,您可以使用
dict.get
而不是[]
,这将不会引发键错误,而是返回None
(或一些默认值),并且所有检查仍然有效
- 如果
检查或
检查变量的结果,则可以使用|=
代替
您可以使用any
检查某些列表中的任何元素是否满足某些条件
您的vt\u结果检查功能的我的版本:
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
你做过调试吗?我们很难浏览一个程序,因为我们必须制作带有名称的示例文件,或者通读整个程序,看看我们是否可以在不运行代码的情况下看到问题。脚本只定义两个函数。你应该运行它们。那我怎么让它工作呢?非常感谢您的帮助?@chevybow我已尝试进行一些调试,但没有收到任何错误消息。你知道该怎么办吗?那些无用的怎么办?甚至在整个函数体中都有一个!你不会看到任何异常,只是想知道你的代码出了什么问题。谢谢你的回复!我让它打印出控制台中第一个.txt文件的结果,但是如何让循环遍历目录中的所有文件呢?我非常感谢你的帮助!!!当然,我更新了我的答案来展示如何做到这一点。或者至少有一种方法可以做到这一点,可能有很多感谢你的回应。我厌倦了运行您的脚本,不幸的是,在第17行和第22行的无效语法上面出现了一些错误消息。我搞不清楚正确的语法是什么。我将非常感谢你的帮助@bedford一行中有一个不匹配的parens,但我找不到另一个语法错误。它现在可以工作了吗,还是出现了运行时错误?如果是,哪一个?谢谢您的回复!如果我设置dunder main并尝试像这样打印vt_result_check:If name='main':print(vt_result_check(path)),那么我会得到以下错误消息:第33行,在print(vt_result_check(path))中,这不是函数内部,而是调用函数的代码。什么是路径
?在原始代码中,您使用了未定义的vt_结果_文件
(而是vt_结果_路径
);也许这里的问题类似?我将路径设置为仅此类型的路径,并在脚本顶部声明它。