我正在开发一个机器学习项目,该项目从所有电子邮件中过滤垃圾邮件/网络钓鱼电子邮件。为此,我使用 SpamAssassin 数据集。数据集包含这种格式的不同邮件:
现在,我识别网络钓鱼/垃圾邮件的首要任务是找出电子邮件中存在的网络链接数量。为此,我编写了以下代码:
wordsInLine = []
tempWord = []
urlList = []
def count():
flag = 0
print("Reading all file names in sorted order")
for filename in sorted(os.listdir("C:/Users/keert/Downloads/Spam_Assassin/spam")):
file=open(filename)
count1 = 0
for line in file:
wordsInLine = line.split(' ')
for word in wordsInLine:
if re.search('href="http',word,re.I):
count1=count1+1
file.close()
urlList.append(count1)
if flag!=0:
print("File Name = " + filename)
print ("Number of links = ",count1)
flag = flag + 1
count()
final = urlList[1:]
print("List of number of links in each email")
print(final)
with open('count_links.csv', 'wb') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
for val in final:
wr.writerow([val])
print("CSV file generated")
注意:我也尝试使用os.path.isfile()
函数检查文件是否存在,但这也返回 false。请有人给我一个解决方案。先感谢您。