Home > Software design >  Writing to a file with mode "w" versus "a" - I can't get "w" to w
Writing to a file with mode "w" versus "a" - I can't get "w" to w

Time:02-08

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)

CodePudding user response:

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:

CodePudding user response:

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)

CodePudding user response:

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 open for the starting_letter.txt to be a read() versus 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()

  •  Tags:  
  • Related