Programming Wiki: Remove X-Spambayes-Trained Tag From Emails

Programming Wiki : SpambayesRemoveXSpambayesTrained

WikiHomePage :: Categories :: PageIndex :: RecentChanges :: RecentlyCommented :: Login/Register

Remove X-Spambayes-Trained Tag From Emails

Python


Move through email directories removing the X-Spambayes-Trained tag from e-mail headers so that Spambayes will retrain on the e-mails. This is usefull if you are starting training fresh.

Second Approach

Use one Python script using os.path.walk method.
Out of time for this right now as os.path.walk isn't as simple as it seems like it should be.
Looked into path.py. May be quicker/easier for this kind of stuff.

First Approach

Messy but works.. I use bash scripts to make the python script work.
This is the python that replaces text from one file and writes the text with the replaced text to another file. It can't write it to the same file that it is reading from.
In practice it would be better to use sed
sed -i 's/x/y/' file_name

to accomplish this. This sed command would find x and replace it with y and save it in the same file that it read it from.

string_replace.py
#!/usr/bin/env python

import os, sys

nargs = len(sys.argv)

if not 3 <= nargs <= 5:
    print "usage: %s search_text replace_text [infile [outfile]]" % \
          os.path.basename(sys.argv[0])
else:
    stext = sys.argv[1]
    rtext = sys.argv[2]
    input = sys.stdin
    output = sys.stdout
    if nargs > 3:
        input = open(sys.argv[3])
        if nargs > 4:
            output = open(sys.argv[4], 'w')
            for s in input.xreadlines():
                output.write(s.replace(stext, rtext))
                pass
            output.close()
            pass
        input.close()



I am going to use find to call this script with the correct parameters to remove the trained spam tag.
Call the python script to replace the text.

exec_string_replace
#!/bin/bash

./string_replace.py 'X-Spambayes-Trained: spam' '' "$1" temp
mv temp "$1"
./string_replace.py 'X-Spambayes-Trained: ham' '' "$1" temp
mv temp "$1"



Now call find command that will execute the bash script above on every e-mail file it finds.. (all of my emails start with 1)
rm_trained_tag
#!/bin/bash
find . -name '1*' -type f -exec ./exec_string_replace {} \;


Calling rm_trained_tag from the base directory of your e-mail will remove the tags.

There are no comments on this page. [Add comment]

Powered by Wikka Wakka Wiki 1.1.6.0
Page was generated in 0.0177 seconds