Merging Mails
Welcome to the Merging Mails Practice Lab. In this module, you will be provided with the instructions and devices needed to develop your hands-on skills.
Already have an account? Sign In »

Introduction
Welcome to the Merging Mails Practice Lab. In this module, you will be provided with the instructions and devices needed to develop your hands-on skills.
Learning Outcomes
In this module, you will complete the following exercise:
- Exercise 1 - Merging Mails
After completing this lab, you will be able to:
- Merge text files in Python programs
Exam Objectives
The following exam objectives are covered in this lab:
- 3.1 Construct and analyze code segments that perform file input and output operations
Lab Duration
It will take approximately 20 minutes to complete this lab.
Exercise 1 - Merging Mails
Mail merge is a simple program that uses two file inputs:
- File 1 contains the recipient names separated by the Enter key.
- File 2 contains the body of the email that you want to write to all recipients.
You need to read the recipient name from file 1 and write it to a new file. Then you read the email contents from file 2 and append it to the new file. You repeat this for all the recipients in file 1 and that gives you personalized emails for all recipients.
To be able to work with files, you need to be aware of the File operations provided by Python. The following constructs are most typically used when handling File I/O (Input/Output) operations:
- open(): This function helps open a file and gets you access to a file object, which is a native data type of Python. File objects contain methods that enable you to read and manipulate that file.
The syntax of the open() function is as follows:
file_object = open(“filename”, “mode”, “encoding”)
In the syntax, the file_object is the variable that will store the file object returned by the open() function. The parameter 1, filename, is the name of the file to be opened. The parameter 2, mode, tells the way the file will be opened and used. The modes can be as follows:
If the mode is not specified, the ‘r’ or read mode is assumed, and the file is opened as a read-only file. The parameter 3, encoding, specifies the encoding type so that the program doesn’t behave differently on different platforms. Encoding is important to specify while working with text files because for a computer; all files are just bytes, not characters. Although if you do not specify the encoding, the default encoding will be picked. However, you run a risk of the default encoding being wrong and not able to express all characters. For most practical purposes, you can specify “UTF-8” as encoding.
- with: The with statement ensures that the file is automatically closed after the code block is executed normally or exited abruptly due to an exception. This, in turn, ensures that the file doesn’t get corrupted.
In this exercise, you will perform two tasks. In the first task, you will create two text files using Notepad. The first text file will contain the recipient names. The second text file will contain the email message.
In the second task, you will learn to write a mail merge program to read from a file containing the body of an email and another file containing the recipient names. You will merge the contents of these two files to create personalized emails for all recipients.
Learning Outcomes
After completing this exercise, you will be able to:
- Merge text files in Python programs
See the full benefits of our immersive learning experience with interactive courses and guided career paths.