Thanks for yoru reply. This was my first attempt,when running through idleid
get the following error:-


Traceback (most recent call last):
  File "C:\Users\Bill\Desktop\TXT_Output\email_extraction_script.py", line
27, in <module>
    traverse_dirs(working_dir)
  File "C:\Users\Bill\Desktop\TXT_Output\email_extraction_script.py", line
20, in traverse_dirs
    if match:
UnboundLocalError: local variable 'match' referenced before assignment

My code is as follows:

import os
import datetime
import re

now = datetime.datetime.now()
timestamp = now.strftime('%d-%m-%Y')
output_file = open('OUTPUT - ' + timestamp + '.csv', 'w+')

def traverse_dirs(wdir):
    grabline = 0
    for f in os.listdir('.'):
        if os.path.isfile(f) == True:
            if "Email Exceeded Maximum Size Limit" in f:
                continue
            else:
                content = open(f)
                lines = content.readlines()
                for line in lines:
                    match =
re.search(r"\b[^\<][A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}[^\>]\b",l
ine)
                if match:
                        print(match.group(0))
                        otext = match.group(0) + ",\n"
                        output_file.write(otext)

if __name__ == "__main__":
    working_dir = 'C:\\Users\\Jono\\Desktop\\TXT_Output\\'
    traverse_dirs(working_dir)

-----Original Message-----
From: Tutor [mailto:tutor-bounces+bill5work=outlook....@python.org] On
Behalf Of Bill
Sent: 10 August 2014 12:31
To: tutor@python.org
Subject: [Tutor] Using Python and Regex


Hi,

I'm relatively new to Python and I'm trying to write a script to iterate
through a series of text files in folder searching for some specific text
and write it to a CSV.

I plan to use Regex to match the text and I have already identified the
Regex to do this. I've also got as far as creating a CSV using python but,
being new to this, I'm looking for some pointers on how to traverse through
the folder and open each file in turn to check the content for matches using
Regex.

Any advice would be gratefully received.

Regards

Bill
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to