Chenyun Zhang
2 min readJan 9, 2021

A mini project with Python to automate your work

My friend reached out to me yesterday asking me if I can generate emails for him. His request is simple. He wants to register a new Gmail account, the format is “Lin****@gmail.com”. The four stars are random numbers. His last name is popular. He tried many numbers, but are all taken, so he wants to send testing emails to see if the email is taken.

I initially wrote a simple function and printed the result to my terminal.

def email():
for i in range(10000):
str_i = len(str(i))
temp = ""
while str_i < 4:
temp+="0"
str_i+=1
temp+=str(i)
print("Lin"+temp+"@gmail.com")
email()

It works fine, but I soon realized that I can’t copy and paste 100000 output from the terminal. I figured I can actually write a text file with python. I sent the text file to my friend. He was asking if there is any way to separate the file into 500 emails each. So I wrote a better one with 500 emails each, and it gives me 20 text files.

def email():
file_number = 0
for i in range(10000):
str_i = len(str(i))
temp = ""
while str_i < 4:
temp+="0"
str_i+=1
temp+=str(i)
if i % 500 == 0:
file_number+=1
f = open('file'+str(file_number)+'.txt',"a")
f.write("Lin"+temp+"@gmail.com,"+"\n")
f.close()
email()

20 text files are a lot. I googled to see if I can zip them with python. Python as a powerful programming language never lets me down. I can use the ZipFile library to zip them into one file with just a few lines of code.

import os
from zipfile import ZipFile
directory = "/Users/chenyunzhang/email"
zipObj = ZipFile('email.zip',"w")
for filename in os.listdir(directory):
if filename.endswith("txt"):
zipObj.write(filename)
zipObj.close()

Make sure you put the python file inside of the text files folder. And done!

Final Thought🤔

I sent the zip file over to my friend, he was amazed how python can do this so quickly and easily. I’m amazed too. What I learned from this mini project is coding is not far away from us. You can use it not only to build a website, apps, or do data analysis but also to write programs to automate the daily work for you.