Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Appending to a File

Give Feedback

Sometimes, you may want to append to a file or start writing at the end of an already populated file. You can do this by using the 'a' character for the mode argument:

with open('dog_breeds.txt', 'a') as a_writer:
    a_writer.write('\nBeagle')

When you examine dog_breeds.txt again, you’ll see that the beginning of the file is unchanged, and Beagle is now added to the end of the file:

>>>
>>> with open('dog_breeds.txt', 'r') as reader:
>>>     print(reader.read())
Pug
Jack Russell Terrier
English Springer Spaniel
German Shepherd
Staffordshire Bull Terrier
Cavalier King Charles Spaniel
Golden Retriever
West Highland White Terrier
Boxer
Border Terrier
Beagle

Cory on April 29, 2020

So on windows os all \n would have to be \r\n?

Ricky White RP Team on April 29, 2020

Hi @Cory. No, \n works fine on Windows, too. It’s what I always use.

Become a Member to join the conversation.