FAQ: Learn Python: Files - Appending to a File

This community-built FAQ covers the “Appending to a File” exercise from the lesson “Learn Python: Files”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Python 3

FAQs on the exercise Appending to a File

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

2 posts were split to a new topic: Why does the code append “Air Buddy” twice?

Aside from code for one sec, is this task a reference to the movie “Air Bud: Golden Receiver” changed slightly to stay safe on the copy-right side of things? :smiley::thinking:
I haven’t watched it myself, just found it while googling (for some reason) to see if Air Buddy was real…:crazy_face:

edit: I watched the trailer, looks super lame. :+1:

why does it print <_io.TextIOWrapper name=‘cool_dogs.txt’ mode=‘a’ encoding=‘UTF-8’> rather than a string

1 Like

I think the explanation: “using the file object’s .write() method appends whatever is passed to the end of the file in a new line.” is not up to date or just wrong.

They are not directly added in new lines, but in the same line. For accessing new lines you could write for example:

with open('cool_dogs.txt', 'a') as cool_dogs_file:
  cool_dogs_file.write("\nAir Buddy")

The explanation of this lesson kind of confused me:

Where did the extra appended line come from? I thought it was only appended once

This was a popular file......
and it still is...
and it still is

Because they run that line 2 times as I remember.

Oh as in the code below, written twice?

with open('generated_file.txt', 'a') as gen_file:
  gen_file.write("... and it still is")

I thought it meant hitting the ‘run’ button twice

with open('cool_dogs.txt', 'a') as cool_dogs_file: a_a = cool_dogs_file.write('Air Buddy') print(a_a)

I just tried to print the text and the output is 9. Shouldn’t it print out the text?

I think it happens when you assign the write method to a variable. Try using that method without assigning it to another variable and then reading the file.

It happened the same to me!

@alice_lelli I assume in this case both you and @freakyclown tried to do this:

with open("cool_dogs.txt", "a") as cool_dogs_file:
  cool_dogs_file.write("Air Buddy\n")

which gives this output:

<_io.TextIOWrapper name='cool_dogs.txt' mode='a' encoding='UTF-8'>

cool_dogs_file is not a string, hence it would not print a string. Instead, because cool_dogs_file is a temporary file object, it prints information about it instead such as the type of file it is (_io.TextIOWrapper), the name, mode it was opened in, etc.

If you want to print the content of the file, you should use either read or readline and print the read content. For example:

with open("cool_dogs.txt", "r") as cool_dogs_file:

In Appending to a File (https://www.codecademy.com/courses/learn-python-3/lessons/learn-python-files/exercises/appending-to-a-file) which is the lesson right after the one where you had a question, when I print out cool_dogs_file, the Air Buddy is outputted three times. But I only hit the run button once. So I don’t understand why Air Buddy would have been appended three times?

with open('cool_dogs.txt', 'a') as cool_dogs_file:
  cool_dogs_file.write('Air Buddy\n')

with open('cool_dogs.txt') as cool_dogs_file:
  cool_dogs = cool_dogs_file.read()

The output of the above code is below …

Snoop Doggy
Air Buddy
Air Buddy
Air Buddy