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:
THIS MOVIE
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")
4 Likes

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.

“Air Buddy is a Golden Retriever that plays basketball”
is that true?
i mean, is that a real dog or just a story?

It happened the same to me!