安然电子邮件数据集垃圾邮件分类

2024-04-25 22:28:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试清除电子邮件上的数据。我已按以下代码读取并显示所有文件:

 import os
 directory = os.path.normpath("C:/Users/cool_/Desktop/Enron/Enron1/ham")
 for subdir, dirs, files in os.walk(directory):
     for file in files:
         if file.endswith(".txt"):
             f=open(os.path.join(subdir, file),'r')
             a = f.read()
             print (a)
             f.close()

我试着用以下方法清理它:

^{pr2}$

但它只清理文件夹中的最后一个文件。 你能帮我在哪里清理所有的文件,并将所有清理后的文件保存在一个新的位置,分类为垃圾邮件或火腿。在

我试着用这个方法:

file_list = os.listdir(r"C:/Users/cool_/Desktop/Enron/enron1")
for i in file_list:
    ham1_html = open(file, 'r').read()
    cleaned_html = clean_html(ham1_html)
    try:
       # create dirs for preprocess file
       pre_path = 'pre' + re.search('/.*/', i).group()
       os.makedirs(pre_path)
    except OSError:
        # ignore exist dirs
        pass
    finally:
        # write preprocess files into pre/ directories
        with open(re.sub('ham1/', 'pre/', i), 'w') as f:
            f.write(cleaned_html)

问候


Tags: 文件pathinforoshtmlfilesopen