Skip to content

Instantly share code, notes, and snippets.

@Marak
Forked from Siddhartha90/anonymize-emails.py
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Marak/a0f1d699eeecc320c42a to your computer and use it in GitHub Desktop.
Save Marak/a0f1d699eeecc320c42a to your computer and use it in GitHub Desktop.
A python script which anonymizes email addresses in all files in current directory and sub-directories.
# A python script which anonymizes email addresses in all files in current directory and sub-directories.
# e.g. A file with the following contents:
# siddhartha@gmail.com
# Sid Phn#- 6385833322
# gupta49@illinois.edu
# weee@as.cd
# sid@yahoo.co.in
# Would change to:
# xxxx@gmail.com
# Sid Phn#- 6385833322
# xxxx@illinois.edu
# xxxx@as.cd
# xxxx@yahoo.co.in
import os
import re
from os.path import join, getsize, isfile
def main():
for root, dirs, files in os.walk('.'):
for filename in files:
if not filename.startswith('.'):
filename = join(root, filename)
myfile = open(filename, 'r')
content = myfile.read()
content = re.sub(r'.+(?=@.+\.(.+))', "xxxx", content)
myfile = open(filename, 'w')
myfile.write(content)
myfile.close()
# Call the main function.
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment