Write mode starts every file from empty. That is right for a file that holds one answer, such as today’s score, but it is wrong for a file that is meant to grow: a class list, a log of results, a guest book. For those, each run of the program has to keep what is already there and add to the end of it.
That is what the third mode is for.
name = "Dave"
my_file = open("appending.txt", "a")
my_file.write(name + "\n")
my_file.close()
"a" opens a file to append to it: to add on at the end.
Say appending.txt holds two names before the program above runs:
Alice
Colin
After the program has run, it holds three:
Alice
Colin
Dave
Run the same program again and there is a second Dave at the end. Append mode
never checks what is already in the file; it only adds.
Append mode adds after the very last character in the file. If the last line
already in the file does not end with a \n, the new text carries on along that
same line: Colin followed by Dave becomes ColinDave. Ending every line you
write with "\n" means the next program to append starts on a fresh line.
| Mode | Used to | If the file does not exist | What is already in the file |
|---|---|---|---|
"r" |
read | the program stops with an error | is read, and left alone |
"w" |
write | the file is created | is wiped as soon as the file is opened |
"a" |
append | the file is created | is kept, and new text goes at the end |
The question to ask before you pick a mode: should the file still hold its old
contents when the program has finished? If not, "w". If so, "a".
A program that appends is often one that keeps going for as long as the user
has more to add. You met loops like that in the chapter on while: the loop
does not know in advance how many times it will run.
The loop below uses a Boolean variable as a flag to decide when to
stop, like words_left in the 2D arrays chapter. repeat holds True or
False, and the loop checks it with repeat == True, which works the same as
while repeat:.
repeat = True
while repeat == True:
print("Enter a guest's name")
guest = input()
guest_file = open("guests.txt", "a")
guest_file.write(guest + "\n")
guest_file.close()
print("Another guest? y/n")
answer = input()
if answer == "n":
repeat = False
guest_file = open("guests.txt", "r")
for line in guest_file:
print(line.strip())
guest_file.close()
repeat starts as True, so the condition repeat == True is true and the
loop runs at least once.n changes repeat to False. The next time the condition
is checked it is false, and the loop ends.True and False have no quotes around them and always start with a capital
letter. "True" in quotes is just a piece of text, and repeat == True would
never be true for it.
Every name in the loop above opens the file, appends one line and closes it again straight away. That keeps each name safe in the file the moment it is typed, even if the program is stopped halfway. The next exercises work the same way.
Make sure that you check for the following things:
"a" when the old contents have to stay, and "w" only when
they may be thrown away."\n".True and False start with a capital letter and have no quotes around them.False is inside the loop, or the loop
never ends.open has a matching close.