507,406 views
7 votes
7 votes
Write a program that will generate a personalized invitation within a text file for each guest in the guest list file using the event information found in the event details file. Put all the generated invitation files in a directory called invitations. Ensure that the name of each invitation file uniquely identifies each guest's invitation

User Kandis
by
2.5k points

1 Answer

25 votes
25 votes

Answer:

Code:

import os # os module to create directory

event_file = open("event_details.txt", "r") # Getting event file

event_details = "" # event details to be stored

for row in event_file: # traversing through the event_file

event_details += row # appending event details

os.mkdir("./invitations") # make directory in the same parent directory

names = open("guest_list.txt", "r") # getting names of the people

for name in names: # traversing through names in guest_list file

name = name.replace('\\', '') # removing the ending '\\' from the name

invitation_msg = "Hi! " + name + ", You are heartly invited in the Ceremony.\\At " + event_details # Generating the invitation message

file_name = '_'.join(name.split(' ')) # Spliting name in space and joining with the '_'

file_path = "./invitations/" + file_name + ".txt" # Generating each file path

invite_file = open(file_path, "w") # Creating the file for each name

invite_file.write(invitation_msg) # Write invitation to file

Output:

Write a program that will generate a personalized invitation within a text file for-example-1
User Nadeeshani William
by
3.0k points