Why does the code append "Air Buddy" twice?

Hi,

I ran this script twice or maybe more:
with open(‘cool_dogs.txt’, ‘a’) as cool_dogs_file:
cool_dogs_file.write(‘Air Buddy’)

I realize that it was neccesary to add a “\n” to this: cool_dogs_file.write(‘Air Buddy \n’) because if you didn’t add it, it just add Air buddyAir buddyAir Buddy, but I’m not sure whether this the correct way or I’m missing something, because in your example it’s showing up something different, the new content is appending a new line for each run:

" generated_file.txt

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

Notice that we’ve appended "... and it still is" to the file a second time! This is because in script.py we opened generated_file.txt in append-mode."

Thanks.

1 Like

So I ran this script once in the exercise:

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

and then looked at the cool_dogs.txt just out of curiosity and it containes:

1   Laika
2   Beethoven
3   Snoop Doggy
4   Clifford
5   Wishbone
6   Air BuddyAir Buddy

My question obviously: why “Air BuddyAir Buddy”? was it executed two times?