I wrote the following script to anonymize e-mail addresses in a txt file:
import io, os, sys
import re
def main():
try:
# Open the file.
myfile = open('emails.txt', 'r')
# Read the file's contents.
content = myfile.read()
content = re.sub(r'.+(?=@.+\.(com|edu))', "xxxx", content)
myfile = open('emails.txt', 'w')
myfile.write(content)
# Close the file.
myfile.close()
except IOError:
print('An error occured trying to read the file.')
except:
print('An error occured.')
main()
I was wondering how I could make this work for all files in a directory and its subdirectories.