Writing to a file with mode "w" versus "a" - I can't get "w" to work properly

89
April 15, 2022, at 4:00 PM

Let me start out by saying I am very new to Python. I'm taking a Udemy bootcamp, so I am not by any means condoning my solution as the best solution, but for the life of me, I cannot figure out why, when I write to the output files, I have to use mode "a" instead of "w". If I use "w", it only writes the last line of the starting_letter.txt.

invited_names.txt is a .txt file attached to the project with the following records. There is no new line \n after the last name in the file:

Aang
Zuko
Appa
Katara
Sokka
Momo
Uncle Iroh
Toph

The starting_letter.txt file is also attached to my project.

Dear [name],
You are invited to my birthday this Saturday.
Hope you can make it!
Joel

The function of the program is to replace [name] from the first record in the starting_letter.txt file with a name from the invited_names.txt file and create an output file for each name in invited_names.txt. I would prefer to use mode "w" so the files get overlaid each time the file runs because mode "a" obviously will append to an existing file if it already exists. I compared my code to the instructor's solution, and I can't see why hers works with "w", but mine doesn't.

invited_names = open("./Input/Names/invited_names.txt")
invited_names_list = invited_names.readlines()
for i in range(len(invited_names_list)):
    invited_names_list[i] = invited_names_list[i].strip()
starting_letter = open("./Input/Letters/starting_letter.txt")
starting_letter_list = starting_letter.readlines()
for name in invited_names_list:
    for line in starting_letter_list:
        new_line = line.replace("[name]", name)
        with open(f"./Output/ReadyToSend/letter_for_{name}.txt", mode="a") as file:
            file.write(new_line)
Answer 1

Updated functional code, please note the line of code that opens the file. In w mode it opens a new file and it should be outside of the for loop. In your code it opens a new file inside the second for loop and gets overwritten in each iteration of second for loop.

invited_names = open("tmp_data/invited.txt")
invited_names_list = invited_names.readlines()
for i in range(len(invited_names_list)):
    invited_names_list[i] = invited_names_list[i].strip()
starting_letter = open("tmp_data/starting_letter.txt")
starting_letter_list = starting_letter.readlines()
for name in invited_names_list:
    with open(f"tmp_data/letter_for_{name}.txt", mode="w") as file:
        for line in starting_letter_list:
            new_line = line.replace("[name]", name)
            file.write(new_line)
Answer 2

Replace

with open(f"./Output/ReadyToSend/letter_for_{name}.txt", mode="a") as file:

with

with open(f"./Output/ReadyToSend/letter_for_{name}.txt", mode="w") as file:
Answer 3

My code was simply not good. :D

I had an unnecessary readlines() call for the starting_letter.txt. I read too much into the instructors hints and overused that method.

I had to remove the second for loop and also change the opening method for starting_letter.txt to be a read() instead of a readlines(). This allowed me to pull in the whole starting_letter.txt and do one edit on it, rather than parsing through every line to do it. I'm a newbie, and it's showing!

starting_letter_list = starting_letter.readlines()

should have been

starting_letter_list = starting_letter.read()
Rent Charter Buses Company
READ ALSO
Selenium python: iterating through dropdown element - number of items varies on page

Selenium python: iterating through dropdown element - number of items varies on page

I have a repository of pages accessible from a Home pageI have a selenium script that currently loops through each of these (a set number of pages) as I would like, telling me if an iframe on the respective page loads certain elements or not

126
How to plot in edges and routes on the same folium in osmnx

How to plot in edges and routes on the same folium in osmnx

I'm trying to plot edges and routes in an interactive map by using: oxplot_graph_folium or ox

117
Capturing stdout result of `flutter test integration_test`

Capturing stdout result of `flutter test integration_test`

For CI, I need to write a program that runs flutter test integration_test and captures the stdout resultIn python the code would be:

95
Replace a pattern in column of a csv file using Python

Replace a pattern in column of a csv file using Python

I am looking for some help with editing CSV in pythonI have a file in below format

127