Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

In a predict and run task you do not write code from scratch. You start from a small piece of working code, work out what you think it will do before you run it, and then run it to check whether you were right.

This time the program prints nothing of its own. Everything it does happens in two text files, so what you predict is what those files hold once it has finished.

The files before the program runs

writing.txt holds two lines:

Bob
Sarah

appending.txt holds two lines as well:

Alice
Colin

Both files end with a newline, so the next thing written after Sarah or Colin would start on a line of its own.

Predict

Study the code below carefully and write down a prediction before you touch the Run button. Your prediction should use the key terms write mode and append mode, and it should say, line by line, exactly what each of the two files holds when the program has finished. You can type your prediction straight into the code as a comment, on a line starting with #.

my_file = open("writing.txt", "w")
my_file.write("Bob")
my_file.close()

name = "Dave"
my_file = open("appending.txt", "a")
my_file.write(name + "\n")
my_file.close()

name = "Zara"
my_file = open("appending.txt", "a")
my_file.write(name + "\n")
my_file.close()

# The lines below only show you what the two files hold now
print("writing.txt:")
my_file = open("writing.txt", "r")
for line in my_file:
    print(line.strip())
my_file.close()

print("appending.txt:")
my_file = open("appending.txt", "r")
for line in my_file:
    print(line.strip())
my_file.close()

The first thirteen lines are the program. The lines below the comment read both files back, so that running the code shows you their contents. Keep them out of your prediction.

Some questions to check your prediction against:

Only once you have written your prediction should you run the code and compare the result with what you expected.

Answer

writing.txt holds a single line, Bob, with no newline after it. Opening it in write mode emptied it, so Bob and Sarah were both wiped, and then the program wrote Bob again. The first line looks the same as before, but only because the program happens to write the same name: Sarah is gone.

appending.txt holds four lines:

Alice
Colin
Dave
Zara

Append mode kept Alice and Colin, and each of the two write lines added one name after the last line already in the file, Dave first because it is written first. Both names end with "\n", so each is on its own line, and a third name appended later would start on a fresh line too.

The first write has no "\n". It makes no visible difference here, since Bob is the only thing in the file, but a program that appended to writing.txt afterwards would carry on along the same line as Bob.

Help! My code doesn't work!

Make sure that you check for the following things:

  • The mode is in quotes and in lower case: "r", "w" or "a".
  • "w" wipes the file when it is opened, and "a" keeps it and adds to the end.
  • Every line you write ends with "\n", or the next piece of text carries on along the same line.
  • The file name includes its extension: "writing.txt", not "writing".
  • Every file that is opened is closed again with .close().